【发布时间】:2019-08-13 14:47:56
【问题描述】:
我们如何在 python 中将多行字符串读取为文件流?
我试过这个:
import io
mystr = """
# comment line
line 1
line 2
line 3
"""
myfile = io.StringIO(mystr)
with open(myfile) as fi:
for line in fi.readlines():
print(line)
但这失败了。
【问题讨论】:
-
你不需要打电话给
open- 把它改成with myfile as fi:docs
标签: python python-3.x io