StringIO
StringIO操作的只能是str??!
## str寫入StringIO
from io import StringIO
f = StringIO()
f.write('hello')
f.write(' ')
f.write('world!')
f.getvalue()
## 讀取StringIO
from io import StringIO
f = StringIO('Hello!\nHi!\nGoodbye!')
while True:
s = f.readline()
if s == '':
break
print(s.strip())
BytesIO
## 二進(jìn)制數(shù)據(jù)寫入BytesIO
from io import BytesIO
f = BytesIO()
f.write('中文'.encode('utf-8'))
f.getvalue()
## 讀取BytesIO
from io import BytesIO
f = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87')
f.read().decode('utf-8')