我需要存根tempfile
and StringIO
看起来很完美。只是这一切都因疏忽而失败:
In [1]: from StringIO import StringIO
In [2]: with StringIO("foo") as f: f.read()
--> AttributeError: StringIO instance has no attribute '__exit__'
提供预设信息而不是读取具有不确定内容的文件的常用方法是什么?
StringIO 模块早于with
陈述。自从 StringIO已在 Python 3 中删除 http://docs.python.org/release/3.0.1/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit无论如何,你可以使用它的替代品,io.BytesIO http://docs.python.org/library/io.html#io.BytesIO:
>>> import io
>>> with io.BytesIO(b"foo") as f: f.read()
b'foo'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)