A module is written in a class and has only one Stringio class, so its available methods are in the class.
Most of the functions in this class are similar to how files are manipulated.
Cases:
Copy the Code code as follows:
#coding =GBK
Import Stringio, Cstringio, sys
s = Stringio.stringio ("Jgood is a handsome boy")
S.write ("Jgood is a handsome boy \ r \ n")
S.write (' okkkk China ')
S.seek (0)
Print S.read ()
#最后4个字节
S.seek (-4, 2)
Print S.read ()
#----Results----
#JGood is a handsome boy
#okkkk中国
#中国
By example, we see the behavior of Stringio, which is basically consistent with file. Stringio provides a way to easily get the data: Stringio. GetValue (). If you use the Read method to get the data in it, you must first set the location of the file pointer through seek.
A Cstringio module is also available in the Python standard module, which behaves as Stringio, but is better than stringio in terms of operational efficiency. But when using the Cstringio module, there are a few points to note: 1. Cstringio.stringio cannot be inherited as a base class; 2. When you create a Cstringio.stringio object, if the initialization function provides initialization data, the newly generated object is read-only. So the following code is wrong: s = Cstringio.stringio ("jgood/n"); S.write ("Oookkk");
----------------------
Copy the Code code as follows:
S=stringio.strngio ([buf])
This instance is similar to the open method, except that it does not generate files on the hard disk, but only buffers; optional parameter buf is a str or Unicode type. It will be stored together with other data that is subsequently written.
----------------------
methods in the Stringio class:
Read
ReadLine
ReadLines
Write
Writelines
GetValue
Truncate
Tell
Seek
Close
Isatty
Flush
----------------------
S.read ([n])
Parameter n defines the read length, int type, and the default state is to read all the data stored in the object s from the current read and write location. After the read is finished, the read-write location is moved.
----------------------
S.readline ([length])
The parameter length qualifies the end position of the read, the int type, and the default state is none: reads from the current read and write location to the next current line with "\ n" as the Terminator. The read-write location is moved.
----------------------
S.readlines ([Sizehint])
The parameter sizehint is of type int, and the default state is to read all rows and return as a list, in addition to reading from the current read and write location to the next current line with "\ n" as the Terminator. The read-write location is moved.
----------------------
S.write (s)
Writes the parameter s to the object s from the read-write location. The parameter S is a str or Unicode type. The read-write location is moved.
----------------------
S.writelines (list)
Writes a list to the object s from a read-write location. The parameter list is a list that has a member of STR or Unicode type. The read-write location is moved.
----------------------
S.getvalue ()
This function does not have parameters to return all the data in the object S.
----------------------
S.truncate ([size])
The data is cut from the reading and writing position, the parameter size limits the clipping length, and the default value is None.
----------------------
S.tell ()
Returns the current read and write location.
----------------------
S.seek (Pos[,mode])
Move the current read and write position to POS, the optional parameter mode is 0 o'clock to move the read and write position to Pos, to 1 o'clock the read-write position from the current position to move the POS length backward, to 2 o'clock the read and write position at the end of the back and then move the POS length; default is 0.
----------------------
S.close ()
The buffer is freed, and after the function is executed, the data is freed and no further action is taken.
---------------------
S.isatty ()
This function always returns 0. Whether or not the Stringio object has been close ().
----------------------
S.flush ()
Refreshes the internal buffer.
----------------------
The return value of Dir (Stringio.stringio) also contains a test function, but ignore it, it doesn't make any sense
=====================================================
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. (Also, Cstringio does not support Unicode encoding)