Note: IO should be used in Python3. Stringio
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.
First, examples
| 12345678910111213141516171819202122 |
import StringIOs = StringIO.StringIO()s.write(‘www.baidu.com\r\n‘)s.write(‘news.realsil.com.cn‘)s.seek(0)print ‘*‘ * 20print s.tell()print s.read()print ‘*‘ * 20print s.tell()print s.read()print ‘*‘ * 20print s.tell()print s.getvalue()print ‘*‘ * 20print s.tell()s.seek(-4,2)print s.read() |
Operation Result:
********************
0
Www.baidu.com
news.realsil.com.cn
********************
34
********************
34
Www.baidu.com
news.realsil.com.cn
********************
34
m.cn
Second, the method in the Stringio class:
- Read
- ReadLine
- ReadLines
- Write
- WriteLine
- 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.
----------------------
Reprint: Stringio module in Python