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')) # 将二进制内容转化文文字
Python:将数据写入到内存
最新推荐文章于 2024-07-18 19:21:27 发布