The StringIO is a class that comes under the scope of the ‘io’ module. This module mainly deals with the I/O operation and tasks. The different sections related to I/O are files, I/O streams, buffers, and other files handled with the help of this ‘io’ module. In Python, if the data is present in the form of a file object, then this StringIO class helps to handle the basic operation over string files. The I/O operations related to the string objects are implemented with the help of this ‘io’ module.
In this article, we will overview the StringIO module and how to use this StringIO module in Python.
IO Module in Python
The ‘io’ module in Python helps to handle and create basic I/O operations on string-like file objects. This module contains various classes which help to implement different techniques on files. Some help to create buffers, class streams, and encoding-decoding tasks, which are similar to the normal files. This is considered a standard file of Python where you implement different functions on file-like objects like normal files.
The StringIO module is used to implement basic tasks on file-like objects. In Python, we can perform different tasks like reading, inserting, delete data from the files. Here we can do the same thing with the help of the StringIO module. This file contains the string data on which we can apply different functions. Let’s see how to use this StringIO module in Python.
import io file = io.StringIO()
This 2-line code creates a file-like structure using the StringIO module. This file variable contains this object on which we can perform different functions.
import io file = io.StringIO() file.write('StringIO is a part of io module') result = file.getvalue() print(result) file.close()
Here, in the 3rd line of code, we have used the .write() function from the StringIO module. This write() function allows us to enter a string in a created file. The 4th line of code uses the .getvalue() function. This function helps to collect all the data from the object created by the StringIO module. On the next line of code, this string data is printed. After operating the data we can close this file using the .close() function from the StringIO module.
Let’s see how this module is executed.
In the result, we can see the normal string is printed as an output.
We can also use different functions from the StringIO module. Let’s see all these functions one by one.
The seek() function in Python is used to set the stream position. If we provide zero value to the seek() function, then it will be set to the beginning of the string.
import io file = io.StringIO() file.write('StringIO is a part of io module') file.seek(0)
In implementing the read() function, you will understand why this seek() function is necessary.
The read() function in StringIO helps to read all the data present in the file-like object created by the StringIO module. This function will read the data from the start position. For this, we need to use seek() function and set this function to zero so that this function will start reading data from the beginning. This seek() function will lead to the read() function in this case. Let’s see how this is executed.
import io file = io.StringIO() file.write('StringIO is a part of io module') file.seek(0) result = file.read() print(result)
Here, seek() and read() functions are used to read the data from the object.
In the results, you can see the string is printed.
The tell() function from the StringIO module will help to identify the position from which the read() function started reading the text. This is used to know the current position in the string.
import io file = io.StringIO() file.write('StringIO is a part of io module') file.seek(1) result = file.tell() print(result)
In this code, on line No. 4, the seek() function is set to 1 so the output should be 1. Let’s see the results.
This truncate() function is used to minimize the string up to provided numbers of characters. We can provide any number it will start from the current position to the position of the number that we have provided. Let’s see the implementation.
import io file = io.StringIO() file.write('StringIO is a part of io module') file.seek(1) result = file.truncate(4) print(result)
Here, the current position is 1, and the truncate() function is set to 1. Let’s see the results.
The readline() function will read a single line from the data present in the file-like structure. This line will be selected by the current position. Let’s see the implementation.
import io file = io.StringIO() file.write('StringIO is a part of io module') file.seek(1) result = file.readline() print(result)
The current position is set to 1, so this function should read the data from the 2nd character of the string.
Here, you can see in the results the string is printed from the second character. The ‘s’ is missing in the printing statement.
This writelines() function helps to write multiple strings in the file-like object. This will provide multiple strings in the form of a list to the file-like object.
import io file = io.StringIO() lines = ['StringIO is a part of io module\n', 'I/O operations\n'] file.seek(0) file.writelines(lines)
Here, the two different strings are provided to the file-like structure.
Why We Need StringIO Module?
The StringIO module is used in many sectors where the file-like structure, string data, and quick actions over files are needed. For example, in testing, this StringIO is used because it is very easy and quick to handle the changes in such type of file. The testing becomes a fast and easy process due to this StringIO module. Additionally, we can perform several necessary actions on the data due to the wide range of functions available in the StringIO module. We can also pass these files to other libraries so the file formats can be easily manipulated.
The most important thing is we can easily perform any basic I/O operation on data due to this file-like structure. This is very convenient as compared to the other methods.
In this article, the information related to StringIO class from the ‘io’ module is covered in detail. The different functions like read(), write(), seek(), tell(), truncate(), and many more are covered with practical examples. This article also highlights the use of the StringIO module in different sectors. Hope you will enjoy this article.
Do read the official documentation for the io module.