Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

io.StringIO vs open() in Python 3

All I could find is this statement:

The easiest way to create a text stream is with open(), optionally specifying an encoding:

f = open("myfile.txt", "r", encoding="utf-8")

In-memory text streams are also available as StringIO objects:

f = io.StringIO("some initial text data")

But this gives no insight at all on when I should use open() over io.StringIO and vice-versa. I know that they do not work exactly the same behind the scene. But why would someone go for open() in Python 3 ?

like image 748
scharette Avatar asked May 18 '18 19:05

scharette


People also ask

What is Python io StringIO ()?

The StringIO module is an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by passing a string to the constructor. If no string is passed the StringIO will start empty.

Why io BytesIO () is used for?

StringIO and BytesIO are methods that manipulate string and bytes data in memory. StringIO is used for string data and BytesIO is used for binary data. This classes create file like object that operate on string data. The StringIO and BytesIO classes are most useful in scenarios where you need to mimic a normal file.

Why is StringIO used?

StringIO gives you file-like access to strings, so you can use an existing module that deals with a file and change almost nothing and make it work with strings. For example, say you have a logger that writes things to a file and you want to instead send the log output over the network.

What is the io module in Python?

Python io module allows us to manage the file-related input and output operations. The advantage of using the IO module is that the classes and functions available allows us to extend the functionality to enable writing to the Unicode data.


1 Answers

The difference is: open takes a file name (and some other arguments like mode or encoding), io.StringIO takes a plain string and both return file-like objects.

Hence:

  • Use open to read files ;
  • Use StringIO when you need a file-like object and you want to pass the content of a string.

An example with StringIO:

import csv
import io

reader = csv.reader(io.StringIO("a,b,c\n1,2,3"))
print ([r for r in reader])
# output [['a', 'b', 'c'], ['1', '2', '3']]

It's very useful because you can use a string where a file was expected.

In the usual case, with a csv file on your disk, you would write something like:

with open(<path/to/file.csv>, ...) as f:
    reader = csv.reader(f, ...)
like image 166
jferard Avatar answered Oct 15 '22 14:10

jferard