The PythonStringIO module reads and writes data in the memory buffer.

Source: Internet
Author: User
This article describes how to read and write data in the memory buffer using the PythonStringIO module. This article describes how to use an instance, StringIO class methods, file operations, and string operation examples, if you need a StringIO class, you can refer to the module that is written in the class. Therefore, its available methods are all in the class.
Most of the functions in this class are similar to File operations.

Example:

The code is 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 ()

# Last 4 bytes
S. seek (-4, 2)
Print s. read ()

# ---- Result ----
# JGood is a handsome boy
# Okkkk China
# 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 ");

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

The code is as follows:


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:

The code is 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 most of the methods of file objects and StringIO are the same, such as read, readline, readlines, write, and writelines, StringIO can be conveniently used as "memory file objects ".

The code is as follows:


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.