Python 用于数据输入的bash-like-herdoc
有时,测试使用内联数据从文件读取数据的脚本(以便数据和代码都位于同一个文件中)会很方便。在Python 用于数据输入的bash-like-herdoc,python,bash,Python,Bash,有时,测试使用内联数据从文件读取数据的脚本(以便数据和代码都位于同一个文件中)会很方便。在bash中,这可以通过herdoc完成: while read l;do echo $l done << EOF test test2 test3 EOF 将input.txt的内容提供给read_file()inline的最佳方式是什么?您可以使用: 上面的代码适用于Python3。在Python2中使用: from StringIO import StringIO from io
bash
中,这可以通过herdoc
完成:
while read l;do
echo $l
done << EOF
test
test2
test3
EOF
将input.txt的内容提供给read_file()
inline的最佳方式是什么?您可以使用:
上面的代码适用于Python3。在Python2中使用:
from StringIO import StringIO
from io import StringIO
f = StringIO('''\
foo
bar
test
''')
read_file(f)
from StringIO import StringIO