Reprint: Stringio module in Python

Source: Internet
Author: User

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:
    1. Read
    2. ReadLine
    3. ReadLines
    4. Write
    5. WriteLine
    6. GetValue
    7. Truncate
    8. Tell
    9. Seek
    10. Close
    11. Isatty
    12. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.