Python StringIO And BytesIO Example

Python StringIO and BytesIO are methods that manipulate string and bytes data in memory, this make memory data manipulation use the consistent API as read and write files. StringIO is used to operate string data, and if you want to manipulate binary data, you need to use BytesIO. This article will give you some example about how to use them.

1. Python StringIO.

When you write python program, you may not read and write data from / to files, you can also read and write data in memory ( for example, read / write data in String object). In this case, you can use python module StringIO to achieve it.

1.1 Write Data To StringIO Object.

  1. Import StringIO module.
  2. Create a StringIO object.
  3. Call it’s write method to write string to it.
# import StringIO module.
>>> from io import StringIO
# Create a StringIO object.
>>> strIO = StringIO()
>>> strIO.write('Good')
4
>>> strIO.write(' Morning!')
9
# getvalue method will return the written string.
>>> print(strIO.getvalue())
Good Morning!

1.2 Read Data From StringIO Object.

# Import StringIO module.
>>> from io import StringIO
# Create a new StringIO object.
>>> strIO = StringIO('Hello \nWorld!')
# Loop and read each line of the StringIO object.
>>> while True:
# Read one line.
... line = strIO.readline()
# If reach the end of the data then jump out of the loop.
... if line == '':
... break
# Print the tripped line data.
... print(line.strip())
...
Hello
World!

2. Python BytesIO.

BytesIO implements read and write bytes data in memory. We create a BytesIO object and then write some bytes data into it. Please note that instead of writing string, you write utf-8 encoded bytes with BytesIO object.

2.1 Write Bytes Data To ByteIO Object.

# Import BytesIO module.
>>> from io import BytesIO
# Create a BytesIO object.
>>> bytIO = BytesIO()
# Write bytes that are utf-8 encoded chine word.
>>> bytIO.write('中文'.encode('utf-8'))
6
# Get the value and print.
>>> print(bytIO.getvalue())
# The value is byte charactor not a string.
b'\xe4\xb8\xad\xe6\x96\x87'

2.2 Read Bytes Data From ByteIO Object.

>>> from io import BytesIO
>>> byteIOObj = BytesIO()
>>> byteIOObj.write('你好'.encode('utf-8'))
byteIOObj.read()
b'\xe4\xb8\xad\xe6\x96\x87'