The stringio module is used to read and write data in the memory buffer.

Source: Internet
Author: User

The module is written in a class and has only one stringio class, so its available methods are all in the class.
Most of the functions in this class are similar to file operations.

Example:
1 # Coding = GBK
2
3 Import stringio, cstringio, sys
4
5 S = stringio. stringio ("jgood is a handsome boy ")
6 s. Write ("jgood is a handsome boy \ r \ n ")
7 S. Write ('okkkk China ')
8 S. Seek (0)
9 print S. Read ()
10
11 # last 4 bytes
12 s. Seek (-4, 2)
13 print S. Read ()
14
15 # ---- result ----
16 # jgood is a handsome boy
17 # okkkk China
18 # China

 

Through the example, we can see the stringio behavior, basically consistent with the file. Stringio provides a method to conveniently obtain the data:Stringio. Getvalue (). If you use the read method to obtain the data, you must first set the "file Pointer" position through seek.

A cstringio module is also provided in the python standard module. Its behavior is basically the same as that of stringio, but its running efficiency is better than that of stringio. Note the following when using the cstringio module: 1. cstringio. stringio cannot be inherited as a base class; 2. create cstringio. when the stringio object is provided by the initialization function, the newly generated object is read-only. The following code is incorrect: S = cstringio. stringio ("jgood/N"); S. Write ("oookkk ");

----------------------
S = stringio. strngio ([Buf])
This instance is similar to the open method. The difference is that it does not generate files on the hard disk, but only stores the files in the buffer zone. The optional parameter Buf is of the STR or Unicode type. It will be stored together with other data written subsequently.
----------------------
Methods In the stringio class:
... ● Read
... ● Readline
... ● Readlines
... ● Write
... ● Writelines
... ● Getvalue
... ● Truncate
... ● Tell
... ● Seek
... ● Close
... ● Isatty
... ● Flush
----------------------
S. Read ([N])
The read length of parameter n is limited to int type. The default status is to read all data stored in object s from the current read/write location. After reading, the read/write location is moved.
----------------------
S. Readline ([length])
The length parameter specifies the end position of the read, int type. The default status is none: read from the current read/write location to the next row with the ending character "\ n. The read/write location is moved.
----------------------
S. readlines ([sizehint])
The sizehint parameter is of the int type. The default status is to read all rows and return them as a list. In addition, it reads the current row ending with "\ n" from the current read/write position. The read/write location is moved.
----------------------
S. Write (s)
Write the parameter S to the object s from the read/write location. The parameter S is of the STR or Unicode type. The read/write location is moved.
----------------------
S. writelines (list)
Write the list to object s from the read/write location. The parameter list is a list with members of the STR or Unicode type. The read/write location is moved.
----------------------
S. getvalue ()
This function has no parameters and returns all data in object S.
----------------------
S. truncate ([size])
Data is cut from the read/write position. The parameter size specifies the cropping length. The default value is none.
----------------------
S. Tell ()
Returns the current read/write location.
----------------------
S. Seek (Pos [, mode])
Move the current read/write position to the POs. If the mode is set to 0, the read/write position is moved to the POs position. If it is set to 1, the read/write position is moved from the current position to the POs length, when the value is 2, the read/write position is placed at the end and the POs length is moved backward. The default value is 0.
----------------------
S. Close ()
Release the buffer. After this function is executed, the data will be released and no operation can be performed.
---------------------
S. isatty ()
This function always returns 0. Whether or not the stringio object has been closed ().
----------------------
S. Flush ()
Refresh the internal buffer.
----------------------
The return value of Dir (stringio. stringio) also contains a test function, but ignore it and it makes no sense.

========================================================== ==================

Stringio is often used as a String cache. It should be advantageous for stringio. Some of its interfaces are consistent with file operations, that is, using the same code, it can be used as a file operation or stringio operation at the same time. For example:

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 most of the Methods of file objects and stringio are the same, such as read, Readline, readlines, write, and writelines, stringio can be very convenient as a "memory file object ".

Import string
Import stringio

S = stringio. stringio ()
S. Write ("aaaa ")
Lines = ['xxxxx', 'bbbbbbbb']
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 implementation, which has better performance, but a little difference, cstringio has no Len and Pos attributes. (Also, cstringio does not support Unicode encoding)

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.