#The diary of a Python learner
Date:16.01.20245
Focus:文件读写strIO和BytesIO
Key:创建IO,然后写入用.write(),用getvalue()读取
Practice:
1.
# -*- coding: utf-8 -*-
from io import StringIO
>>> f = StringIO()
>>> f.write('hello')
5
>>> f.write(' ')
1
>>> f.write('world!')
6
>>> print(f.getvalue())
hello world!
2.
# -*- coding: utf-8 -*-
from io import BytesIO
>>> f = BytesIO()
>>> f.write('中文'.encode('utf-8'))
6
>>> print(f.getvalue())
b'\xe4\xb8\xad\xe6\x96\x87'
Difficulty?
Addition: