{"id":111663,"date":"2018-02-09T07:28:21","date_gmt":"2018-02-09T07:28:21","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=111663"},"modified":"2019-10-01T11:35:25","modified_gmt":"2019-10-01T11:35:25","slug":"using-io-for-creating-file-object","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/","title":{"rendered":"Python: Using StringIO and BytesIO for managing data as  file object"},"content":{"rendered":"<p>Using buffer modules(StringIO, BytesIO, cStringIO) we can impersonate string or bytes data like a file.These buffer modules help us to mimic our data like a normal file which we can further use for processing.<\/p>\n<p>In python, while processing the I\/O operation of various types( like the text I\/O, binary I\/O and raw I\/O.) many time we deal with the data stream(a file-like object).<\/p>\n<p>&nbsp;<\/p>\n<p>Till python2.7 we were using cStringIO or StringIO while dealing with these data steam.Now in Python 3.x, we are using io.StringIO or io.BytesIO from the io module, as the StringIO, and cStringIO modules are no longer available in Python 3.x.<\/p>\n<blockquote><p>In Python 2.7&nbsp; StringIO module was capable handling the<strong> Byte as well Unicode<\/strong> But in python3 you will have to use separate BytesIO for handling <strong>Byte strings<\/strong> and StringIO for handling <strong>Unicode strings<\/strong>.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<ul>\n<li>io.StringIO requires a Unicode string.<\/li>\n<li>io.BytesIO requires a bytes string.<\/li>\n<li>StringIO.StringIO allows either Unicode or Bytes string.<\/li>\n<li>cStringIO.StringIO requires a string that is encoded as a bytes string.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Here is a simple example using io module<\/p>\n<pre class=\"brush:py\">&gt;&gt;&gt; import io\n&gt;&gt;&gt; string_out = io.StringIO()\n&gt;&gt;&gt; string_out.write('A sample string which we have to send to server as string data.')\n63##Length of data\n&gt;&gt;&gt; string_out.getvalue()\n'A sample string which we have to send to server as string data.'\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Here each successive write append the data in the&nbsp; stream object just like file<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush:py\">&gt;&gt;&gt; string_out = io.StringIO()\n&gt;&gt;&gt; string_out.write('Example String 1 ')\n17\n&gt;&gt;&gt; string_out.write('Example String 2 ')\n17\n&gt;&gt;&gt; string_out.write('Example String 3 ')\n17\n&gt;&gt;&gt; string_out.getvalue()\n'Example String 1 Example String 2 Example String 3 '\n<\/pre>\n<p>&nbsp;<\/p>\n<p>These methods of requires special mentions:<\/p>\n<p style=\"text-align: left\"><strong>getvalue()<\/strong><\/p>\n<p style=\"text-align: left;padding-left: 30px\">Retrieve the entire contents of the &#8220;file&#8221; at any time before the file object&#8217;s close() method is called.<\/p>\n<p><strong>close()<\/strong><\/p>\n<p style=\"padding-left: 30px\">Free the memory buffer and work done with the buffer object.<\/p>\n<p><strong>seek()<\/strong><\/p>\n<p style=\"padding-left: 30px\">You can use seek to move the cursor over it data like seek(0) for start of file<\/p>\n<p>&nbsp;<\/p>\n<p>For more detailed information please visit the <a href=\"https:\/\/docs.python.org\/3\/library\/io.html\">official documentation<\/a>.<\/p>\n<p>That\u2019s all for today. I hope this blog will help you. I\u2019d be very grateful if you\u2019d write your opinions, comments , and suggestions to keep the page updated and interesting.<\/p>\n<p>You may also like our post on&nbsp;<a href=\"https:\/\/webkul.com\/blog\/string-and-bytes-conversion-in-python3-x\/\">Performing String and Bytes Data Conversion in Python3.x<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using buffer modules(StringIO, BytesIO, cStringIO) we can impersonate string or bytes data like a file.These buffer modules help us to mimic our data like a normal file which we can further use for processing. In python, while processing the I\/O operation of various types( like the text I\/O, binary I\/O and raw I\/O.) many time <a href=\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":86,"featured_media":45511,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2007],"tags":[6151,6150,6152,3324,5613,6149],"class_list":["post-111663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odoo","tag-bytesio","tag-cstringio","tag-io","tag-python","tag-python3","tag-stringio"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Using buffer modules(StringIO, BytesIO, cStringIO) we can impersonate string or bytes data like a file.These buffer modules help us to mimic our data like a normal file which we can further use for processing. In python, while processing the I\/O operation of various types( like the text I\/O, binary I\/O and raw I\/O.) many time [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-09T07:28:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-01T11:35:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prakash Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prakash Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\"},\"author\":{\"name\":\"Prakash Kumar\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b\"},\"headline\":\"Python: Using StringIO and BytesIO for managing data as file object\",\"datePublished\":\"2018-02-09T07:28:21+00:00\",\"dateModified\":\"2019-10-01T11:35:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\"},\"wordCount\":333,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png\",\"keywords\":[\"BytesIO\",\"cStringIO\",\"io\",\"python\",\"python3\",\"StringIO\"],\"articleSection\":[\"Odoo\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\",\"url\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\",\"name\":\"Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png\",\"datePublished\":\"2018-02-09T07:28:21+00:00\",\"dateModified\":\"2019-10-01T11:35:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png\",\"width\":825,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: Using StringIO and BytesIO for managing data as file object\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b\",\"name\":\"Prakash Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prakash Kumar\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/prakash-kumar163\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/","og_locale":"en_US","og_type":"article","og_title":"Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog","og_description":"Using buffer modules(StringIO, BytesIO, cStringIO) we can impersonate string or bytes data like a file.These buffer modules help us to mimic our data like a normal file which we can further use for processing. In python, while processing the I\/O operation of various types( like the text I\/O, binary I\/O and raw I\/O.) many time [...]","og_url":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-02-09T07:28:21+00:00","article_modified_time":"2019-10-01T11:35:25+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png","type":"image\/png"}],"author":"Prakash Kumar","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prakash Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/"},"author":{"name":"Prakash Kumar","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b"},"headline":"Python: Using StringIO and BytesIO for managing data as file object","datePublished":"2018-02-09T07:28:21+00:00","dateModified":"2019-10-01T11:35:25+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/"},"wordCount":333,"commentCount":4,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png","keywords":["BytesIO","cStringIO","io","python","python3","StringIO"],"articleSection":["Odoo"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/","url":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/","name":"Python: Using StringIO and BytesIO for managing data as file object - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png","datePublished":"2018-02-09T07:28:21+00:00","dateModified":"2019-10-01T11:35:25+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/04\/Odoo-Code-Snippet-banner.png","width":825,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/using-io-for-creating-file-object\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python: Using StringIO and BytesIO for managing data as file object"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/8b41d806aa1c7ec81f958437fac62e5b","name":"Prakash Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0423ba8e78c94fb466768b44982d402aec0a1bb0ea274e7907672f740c41d776?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prakash Kumar"},"url":"https:\/\/webkul.com\/blog\/author\/prakash-kumar163\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/111663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=111663"}],"version-history":[{"count":16,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/111663\/revisions"}],"predecessor-version":[{"id":200901,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/111663\/revisions\/200901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/45511"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=111663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=111663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=111663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}