Python:将数据写入到内存

from io import StringIO,BytesIO

# StringIO
s = StringIO()
s.write('hello') # 将数据写入到内存
print(s.getvalue()) # 从内存中读取数据

print('你好',file = open('print_test.txt','w'))
print('你好第二遍',file = s) # 将数据写入到内存
print(s.getvalue()) # 从内存中读取数据

# BytesIO
b = BytesIO()
b.write('你好吗'.encode('utf8')) # 将文字转化为二进制
print(b.getvalue().decode('utf8')) # 将二进制内容转化文文字

猜你喜欢

转载自blog.csdn.net/weixin_42161670/article/details/113576676