{"id":1580,"date":"2024-02-25T01:28:57","date_gmt":"2024-02-24T22:28:57","guid":{"rendered":"https:\/\/moito.com\/blog\/?p=1580"},"modified":"2024-02-25T01:29:34","modified_gmt":"2024-02-24T22:29:34","slug":"python-snippets-2024-q1","status":"publish","type":"post","link":"https:\/\/moito.com\/blog\/python-snippets-2024-q1\/","title":{"rendered":"Python snippets 2024 Q1"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">How to convert StringIO object to BytesIO object vice versa.<\/h2>\n\n\n\n<p>I want to read a string with the io.StringIO and then convert it to an io.BytesIO object and vice versa, how can I do this?<\/p>\n\n\n\n<p>Below is the example source code which can implement python StringIO and BytesIO object converts.<\/p>\n\n\n\n<pre class=\"wp-block-code has-beige-background-color has-background\"><code>import io\n# Convert a StringIO object to BytesIO object.\ndef stringio_to_bytesio(str):\n    \n    str_io_object = io.StringIO(str)\n    \n    str_data = str_io_object.read().encode('utf8')\n    \n    bytes_io_object = io.BytesIO(str_data)\n    \n    print(bytes_io_object)  \n    print(bytes_io_object.read())\n    \n# Use io.TextIOWrapper to convert BytesIO object to a string. Then we can build a StringIO object on the string.     \ndef bytesio_to_stringio(bytes_str):\n    \n    data = io.BytesIO(bytes_str)\n    \n    # Create an instance of io.TextIOWrapper class.\n    text_wrapper = io.TextIOWrapper(data, encoding='utf-8')\n    \n    str = text_wrapper.read()\n    \n    str_io_object = io.StringIO(str)\n    print(str_io_object)  \n    \n    print(str)  \nif __name__ == '__main__':\n    \n    bytes_str = stringio_to_bytesio('I love python')\n    \n    bytesio_to_stringio(b'hello python')<\/code><\/pre>\n\n\n\n<p>Source (with more details): <a href=\"https:\/\/www.code-learner.com\/python-stringio-and-bytesio-example\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.code-learner.com\/python-stringio-and-bytesio-example\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to convert StringIO object to BytesIO object vice versa. I want to read a string with the io.StringIO and then convert it to an io.BytesIO object and vice versa, how can I do this? Below is the example source code which can implement python StringIO and BytesIO object converts. Source (with more details): https:\/\/www.code-learner.com\/python-stringio-and-bytesio-example\/<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,10],"tags":[811,810,808,812,809],"class_list":["post-1580","post","type-post","status-publish","format-standard","hentry","category-it","category-10","tag-811","tag-programming","tag-python","tag-q1","tag-snippets"],"_links":{"self":[{"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/posts\/1580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/comments?post=1580"}],"version-history":[{"count":2,"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/posts\/1580\/revisions"}],"predecessor-version":[{"id":1582,"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/posts\/1580\/revisions\/1582"}],"wp:attachment":[{"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/media?parent=1580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/categories?post=1580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moito.com\/blog\/wp-json\/wp\/v2\/tags?post=1580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}