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 ");