您当前的位置: 首页 >  Python

Python编程:StringIO和BytesIO内存中读写操作

彭世瑜 发布时间:2018-06-04 14:26:14 ,浏览量:4

StringIO
from io import StringIO

#像文件一样写入
f = StringIO()
f.write("some words")
f.write("other words")
print(f.getvalue())  # some wordsother words
f.close()

# 初始化,然后,像读文件一样读取
f1 = StringIO("code")
print(f1.read())  # code
f1.close()
BytesIO
from io import BytesIO

fb = BytesIO()
fb.write("中国".encode("utf-8"))
fb.write("美丽".encode("utf-8"))
print(fb.getvalue().decode("utf-8"))
# 中国美丽
fb.close()

# 像读文件一样读取
fb1 = BytesIO("中国".encode("utf-8"))
print(fb1.read())
# b'\xe4\xb8\xad\xe5\x9b\xbd'
fb1.close()
关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 4浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0480s