Python module learning-stringio, cstringio memory files

Source: Internet
Author: User

The stringio behavior is very similar to the file object, but it is not a file on the disk, but a "file" in the memory. We can operate stringio like operating disk files. A simple example gives you a perceptual knowledge of stringio:

1 # Coding = GBK
2  
3 Import Stringio, Cstringio,Sys
4  
5 S =Stringio.Stringio("JgoodIsA handsome boy ")
6 S. Write ("jgoodIsA handsome boy/R/N ")
7 S. Write ('okkkk China ')
8 S. Seek (0)
9 PrintS. Read ()
10  
11 # Last 4 bytes
12 S. Seek (-4,2)
13 PrintS. 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 ");

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.