每天CookBook之Python-078

  • 对字符串执行I/O操作
import io

s = io.StringIO()
s.write(u'Hello World\n')

print('This is a test', file=s)
print(s.getvalue())

s = io.StringIO(u'Hello World\n')

print(s.read(4))
print(s.read())

s = io.BytesIO()
s.write(b'binary data')

print(s.getvalue())

out

Hello World
This is a test

Hell
o World

b'binary data'
posted @ 2016-07-24 12:52  4Thing  阅读(85)  评论(0编辑  收藏  举报