Back to Top

Python: Using StringIO and BytesIO for managing data as file object

Updated 1 October 2019

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 we deal with the data stream(a file-like object).

 

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.

In Python 2.7  StringIO module was capable handling the Byte as well Unicode But in python3 you will have to use separate BytesIO for handling Byte strings and StringIO for handling Unicode strings.

 

Searching for an experienced
Odoo Company ?
Find out More
  • io.StringIO requires a Unicode string.
  • io.BytesIO requires a bytes string.
  • StringIO.StringIO allows either Unicode or Bytes string.
  • cStringIO.StringIO requires a string that is encoded as a bytes string.

 

Here is a simple example using io module

>>> import io
>>> string_out = io.StringIO()
>>> string_out.write('A sample string which we have to send to server as string data.')
63##Length of data
>>> string_out.getvalue()
'A sample string which we have to send to server as string data.'

 

Here each successive write append the data in the  stream object just like file

 

>>> string_out = io.StringIO()
>>> string_out.write('Example String 1 ')
17
>>> string_out.write('Example String 2 ')
17
>>> string_out.write('Example String 3 ')
17
>>> string_out.getvalue()
'Example String 1 Example String 2 Example String 3 '

 

These methods of requires special mentions:

getvalue()

Retrieve the entire contents of the “file” at any time before the file object’s close() method is called.

close()

Free the memory buffer and work done with the buffer object.

seek()

You can use seek to move the cursor over it data like seek(0) for start of file

 

For more detailed information please visit the official documentation.

That’s all for today. I hope this blog will help you. I’d be very grateful if you’d write your opinions, comments , and suggestions to keep the page updated and interesting.

You may also like our post on Performing String and Bytes Data Conversion in Python3.x.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


4 comments

  • Lợi Nguyễn Văn
    • Anisha Bahukhandi (Moderator)
  • Hamed
    • Anisha Bahukhandi (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home