Python Stringio module realizes reading and writing data in memory buffer _python

Source: Internet
Author: User
Tags flush readline python stringio

A module is written with 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 the methods used to manipulate files.

Cases:

Copy 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 in it: Stringio. GetValue (). If you use the Read method to get the data in it, you must first set the location of the file pointer by seek.

The Python standard module also provides a Cstringio module, which behaves in much the same way as Stringio, but has a better operational efficiency than Stringio. But when using the Cstringio module, there are several points to note: 1. Cstringio.stringio cannot be inherited as a base class; 2. When you create a Cstringio.stringio object, the newly generated object is read-only if the initialization function provides initialization data. So the following code is wrong: s = Cstringio.stringio ("jgood/n"); S.write ("Oookkk");

----------------------

Copy Code code as follows:

S=stringio.strngio ([buf])

This instance is similar to the open method, except that it does not generate a file on the hard disk, but only a buffer exists; the optional parameter buf is a str or Unicode type. It will be stored with other subsequent written data.
----------------------
methods in the Stringio class:
Read
ReadLine
ReadLines
Write
Writelines
GetValue
Truncate
Tell
Seek
Close
Isatty
Flush
----------------------
S.read ([n])
Parameter n qualifies read length, int type, and the default state reads all data stored in the object s from the current read-write location. The read and write positions are moved after the reading is finished.
----------------------
S.readline ([length])
Parameter length limits the end position of the read, the int type, and the default state of None: reads from the current read-write location to the next current line with "\ n" as the Terminator. The read-write position is moved.
----------------------
S.readlines ([Sizehint])
The parameter sizehint is the int type, the default state is to read all rows and return as a list, in addition to reading from the current read-write position to the next current row with "\ n" as the Terminator. The read-write position is moved.
----------------------
S.write (s)
Writes the parameter s to object s from the read-write location. The parameter S is a str or Unicode type. The read-write position is moved.
----------------------
S.writelines (list)
Writes a list to object s from a read-write location. The parameter list is a list with a member of STR or Unicode type. The read-write position is moved.
----------------------
S.getvalue ()
This function has no arguments and returns all the data in object S.
----------------------
S.truncate ([size])
Disconnect data from the read-write position, the parameter size limits the clipping length, and 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, optional parameter mode is 0 o'clock to move the read and write position to the POS, to the 1 o'clock read and write position from the current position to move back to the POS length, for 2 o'clock to place the read and write position at the end and then move the POS length; default is 0.
----------------------
S.close ()
Frees the buffer, and when this function is executed, the data is freed and cannot be manipulated.
---------------------
S.isatty ()
This function always returns 0. Regardless of whether the Stringio object has been closed ().
----------------------
S.flush ()
Refreshes the internal buffer.
----------------------
The return value of Dir (Stringio.stringio) also contains a test function, but it doesn't make any sense to ignore it.

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

Stringio is often used as a buffer for strings, and there should be a benefit for stringio, and some of his interfaces and file operations are consistent, that is, with the same code, they can be both file operations or Stringio operations. Like what:

Copy 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, so that Stringio can be very convenient as a "Memory file object."

Copy 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 a little bit of difference, Cstringio 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.