python - StringIO- read in memory str

StringIO

In many cases, the data read and write is not necessarily a file, you can also read and write in memory. StringIO is read in memory str

from IO Import StringIO 

F = StringIO ()     # Create Object StringIO 
I = f.write ( ' Hello ' )   # writes the string 
# Return Value: returns a string number 
I = f.write ( ' Li ' ) 
STR = f.getvalue ()   # read the string 

FF = the StringIO ( ' the Hello! \ Nhi! \ nGoodbye! ' ) 
S = ff.readline ()   # reads a line 
Print (S)

 

 

BytesIO 

StringIO operation can only be str, if you are working with binary data, you need to use BytesIO

BytesIO achieve read and write bytes in memory

 

 

 

Guess you like

Origin www.cnblogs.com/liming19680104/p/12150686.html