Python的IO流-2
这一节也很简单,只是注意一下操作二进制文件需要使用BytesIO
1 from io import StringIO 2 from io import BytesIO 3 f = StringIO() 4 f.write('Hello') 5 f.write(' ') 6 f.write('World') 7 value = f.getvalue() 8 print(value) 9 10 ff = StringIO('Hey,how are you buddy?') 11 while True: 12 s = ff.readline() 13 if s == '': 14 break 15 print(s.strip()) 16 17 b = BytesIO() 18 b.write('浙江大学'.encode('utf-8')) 19 value2 = b.getvalue() 20 print(value2)