Stringio is often used as a buffer for strings, and there should be a benefit for stringio, and some of his interfaces and file operations are consistent, that is, with the same code, they can be both file operations or Stringio operations. Like what:
Copy Code code as follows:
Import string, OS, sys
Import Stringio
def writedata (FD, MSG):
Fd.write (msg)
f = open (' Aaa.txt ', ' W ')
WriteData (F, "xxxxxxxxxxxx")
F.close ()
s = Stringio.stringio ()
WriteData (S, "xxxxxxxxxxxxxx")
Because the file object and Stringio most of the methods are the same, such as read, ReadLine, ReadLines, write, writelines are all, so that Stringio can be very convenient as a "Memory file object."
Copy Code code as follows:
Import string
Import Stringio
s = Stringio.stringio ()
S.write ("AAAA")
lines = [' xxxxx ', ' bbbbbbb ']
S.writelines (lines)
S.seek (0)
Print S.read ()
Print S.getvalue ()
S.write ("Ttttttttt")
S.seek (0)
Print S.readlines ()
Print S.len
Stringio also has a corresponding C language version of the implementation, it has better performance, but a little bit of difference, Cstringio no len and POS attributes.