Stringio is often used as a string cache, should have a benefit for Stringio, some of his interfaces and file operations are consistent, that is, with the same code, can be used as a file operation or Stringio operation. Like what:
Copy the 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 there, so that Stringio can be very convenient as a "Memory file object."
Copy the 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 slightly a little bit of the difference, Cstringio no len and POS attributes.