python中的StringIO模块

Python
#coding=utf-8 import StringIO, cStringIO, sys s = StringIO.StringIO("JGood is a handsome boy") s.write("JGood is a handsome boy \r\n") s.write('okkkk中国') s.seek(0) print s.read() #最后4个字节 s.seek(-4, 2) print s.read() #---- 结果 ---- #JGood is a handsome boy #okkkk中国 #中国 s=StringIO.StrngIO([buf]) #此实例类似于open方法,不同的是它并不会在硬盘中生成文件,而只寄存在缓冲区;可选参数buf是一个str或unicode类型。它将会与其他后续写入的数据存放在一起。 StringIO类中的方法: ....● read ....● readline ....● readlines ....● write ....● writelines ....● getvalue ....● truncate ....● tell ....● seek ....● close ....● isatty ....● flush s.read([n]) 参数n限定读取长度,int类型;缺省状态为从当前读写位置读取对象s中存储的所有数据。读取结束后,读写位置被移动。 s.readline([length]) 参数length限定读取的结束位置,int类型,缺省状态为None:从当前读写位置读取至下一个以“\n”为结束符的当前行。读写位置被移动。 s.readlines([sizehint]) 参数sizehint为int类型,缺省状态为读取所有行并作为列表返回,除此之外从当前读写位置读取至下一个以“\n”为结束符的当前行。读写位置被移动。 s.write(s) 从读写位置将参数s写入给对象s。参数s为str或unicode类型。读写位置被移动。 s.writelines(list) 从读写位置将list写入给对象s。参数list为一个列表,列表的成员为str或unicode类型。读写位置被移动。 s.getvalue() 此函数没有参数,返回对象s中的所有数据。 s.truncate([size]) 从读写位置起切断数据,参数size限定裁剪长度,缺省值为None。 s.tell() 返回当前读写位置。 s.seek(pos[,mode]) 移动当前读写位置至pos处,可选参数mode为0时将读写位置移动至pos处,为1时将读写位置从当前位置起向后移动pos个长度,为2时将读写位置置于末尾处再向后移动pos个长度;默认为0。 s.close() 释放缓冲区,执行此函数后,数据将被释放,也不可再进行操作。 s.isatty() 此函数总是返回0。不论StringIO对象是否已被close()。 s.flush() 刷新内部缓冲区。 dir(StringIO.StringIO)的返回值中还包含有一个test函数,不过不用理睬它,它没有任何意义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#coding=utf-8
 
import StringIO , cStringIO , sys
 
s = StringIO . StringIO ( "JGood is a handsome boy" )
s . write ( "JGood is a handsome boy \r\n" )
s . write ( 'okkkk中国' )
s . seek ( 0 )
 
print s . read ( )
 
#最后4个字节 s.seek(-4, 2) print s.read()
 
#---- 结果 ---- #JGood is a handsome boy #okkkk中国 #中国
 
s = StringIO . StrngIO ( [ buf ] )
 
#此实例类似于open方法,不同的是它并不会在硬盘中生成文件,而只寄存在缓冲区;可选参数buf是一个str或unicode类型。它将会与其他后续写入的数据存放在一起。 StringIO类中的方法:
 
. . . .● read
 
. . . .● readline
 
. . . .● readlines
 
. . . .● write
 
. . . .● writelines
 
. . . .● getvalue
 
. . . .● truncate
 
. . . .● tell
 
. . . .● seek
 
. . . .● close
 
. . . .● isatty
 
. . . .● flush
 
s . read ( [ n ] )
 
参数 n限定读取长度, int类型;缺省状态为从当前读写位置读取对象 s中存储的所有数据。读取结束后,读写位置被移动。
 
s . readline ( [ length ] )
 
参数 length限定读取的结束位置, int类型,缺省状态为 None:从当前读写位置读取至下一个以“ \ n”为结束符的当前行。读写位置被移动。
 
s . readlines ( [ sizehint ] )
 
参数 sizehint为 int类型,缺省状态为读取所有行并作为列表返回,除此之外从当前读写位置读取至下一个以“ \ n”为结束符的当前行。读写位置被移动。
 
s . write ( s )
 
从读写位置将参数 s写入给对象 s。参数 s为 str或 unicode类型。读写位置被移动。
 
s . writelines ( list )
 
从读写位置将 list写入给对象 s。参数 list为一个列表,列表的成员为 str或 unicode类型。读写位置被移动。
 
s . getvalue ( )
 
此函数没有参数,返回对象 s中的所有数据。
 
s . truncate ( [ size ] )
 
从读写位置起切断数据,参数 size限定裁剪长度,缺省值为 None。
 
s . tell ( )
 
返回当前读写位置。
 
s . seek ( pos [ , mode ] )
 
移动当前读写位置至 pos处,可选参数 mode为 0时将读写位置移动至 pos处,为 1时将读写位置从当前位置起向后移动 pos个长度,为 2时将读写位置置于末尾处再向后移动 pos个长度;默认为 0。
 
s . close ( )
 
释放缓冲区,执行此函数后,数据将被释放,也不可再进行操作。
 
s . isatty ( )
 
此函数总是返回 0。不论 StringIO对象是否已被 close ( )。
 
s . flush ( )
 
刷新内部缓冲区。
 
dir ( StringIO . StringIO )的返回值中还包含有一个 test函数,不过不用理睬它,它没有任何意义
 



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值