1Jan

Stringio Python3

1 Jan 2000admin

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.

StringIO — Read and write strings as files¶. This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).See the description of file objects for operations (section File Objects). (For standard strings, see str and unicode.). Class StringIO.StringIO(buffer)¶. When a StringIO object is created, it can be initialized to an.

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.

Happy tree friends false alarm pc download. ^ Goldstein, Hilary (June 26, 2008). Retrieved October 22, 2015. Retrieved October 22, 2015.

  • 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

2
4
6
>>>string_out=io.StringIO()
>>>string_out.write('A sample string which we have to send to server as string 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


2
4
6
8
>>>string_out.write('Example String 1 ')
>>>string_out.write('Example String 2 ')
>>>string_out.write('Example String 3 ')
>>>string_out.getvalue()
'Example String 1 Example String 2 Example String 3 '


Subscribe Articles

Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

You've Subscribed!

Share article via

SignUp Again

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()

Three super heroes—Death, Halloween and devil Rock-n-Roll—have arrived to overthrow the dictator. Armed with magical weapons, they will take back the world and remind the people that imagination and dreams always win in the end. Choose from a large selection of weapons; Variety of abilities and power-ups. Rip strike back.

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.

Some code: import cStringIOdef f:buffer = cStringIO.StringIObuffer.write('something')return buffer.getvalueThe says:StringIO.close: Free the memory buffer. Attempting to do furtheroperations with a closed StringIO object will raise a ValueError.Do I have to do buffer.close, or it will happen automatically when buffer goes out of scope and is garbage collected?UPDATE:I did a test: import StringIO, weakrefdef handler(ref):print 'Buffer died!'

Def f:buffer = StringIO.StringIOref = weakref.ref(buffer, handler)buffer.write('something')return buffer.getvalueprint 'before f'fprint 'after f'Result: vic@wic:/projects$ python test.pybefore fBuffer died!after fvic@wic:/projects$. Generally it's still better to call close or use the with statement, because there may be some unexpected behaviour in special circumstances. For example, the expat- IncrementalParser seems to expect a file to be closed, or it won't return the last tidbit of parsed xml until a timeout occurs in some rare circumstances.But for the with-statement, which handles the closing for you, you have to use the StringIO class from the io-Modules, as stated in the comment of Ivc.This was a major headache in some legacy sax-parser script we solved by closing the StringIO manually.The 'out-of-scope' close didn't work. It just waited for the timeout-limit.