Python StringIO And BytesIO Example

Python StringIO and BytesIO are methods that manipulate string and bytes data in memory, this makes 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 examples of how to use them.

1. Python StringIO.

When you write a 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 the 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 a 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 a string, you write utf-8 encoded bytes with the 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'

3. Question & Answer.

3.1 How to convert StringIO object to BytesIO object vice versa.

  1. 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?
  2. Below is the example source code which can implement python StringIO and BytesIO object converts.
    import io
    
    # Convert a StringIO object to BytesIO object.
    def stringio_to_bytesio(str):
        
        str_io_object = io.StringIO(str)
        
        str_data = str_io_object.read().encode('utf8')
        
        bytes_io_object = io.BytesIO(str_data)
        
        print(bytes_io_object)  
    
        print(bytes_io_object.read())
        
    
    # Use io.TextIOWrapper to convert BytesIO object to a string. Then we can build a StringIO object on the string.     
    def bytesio_to_stringio(bytes_str):
        
        data = io.BytesIO(bytes_str)
        
        # Create an instance of io.TextIOWrapper class.
        text_wrapper = io.TextIOWrapper(data, encoding='utf-8')
        
        str = text_wrapper.read()
        
        str_io_object = io.StringIO(str)
    
        print(str_io_object)  
        
        print(str)  
    
    if __name__ == '__main__':
        
        bytes_str = stringio_to_bytesio('I love python')
        
        bytesio_to_stringio(b'hello python')
    
  3. Below is the above source code execution result.
    <_io.BytesIO object at 0x7f8f3397de30>
    b'I love python'
    <_io.StringIO object at 0x7f8f33a847d0>
    hello python
    

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clicky