对于Python
2.x,请使用StringIO模块。例如:
>>> from cStringIO import StringIO>>> f = StringIO('foo')>>> f.read()'foo'我使用cStringIO(速度更快),但请注意,它不接受无法编码为纯ASCII字符串的Unipre字符串。(您可以通过将“ from cStringIO”更改为“ from StringIO”来切换到StringIO。)
对于Python 3.x,请使用
io模块。
f = io.StringIO('foo')


