stdin stdout python: how to reuse the same input file twice?

余生颓废 提交于 2019-12-02 07:17:34

You are trying to use an open file object as a filename:

filename = sys.stdin

# ...

input_file = open(filename, 'rU')

You cannot re-read from sys.stdin anyway; you need to read all of the file into memory, then process it twice:

if filename == '-':
    input_file = sys.stdin
else:
    input_file = open(filename, 'rU')

input_data = input_file.read()

tree = etree.fromstring(input_data)
extract(tree)

change_class(input_data)

mwhere you'll have to alter change_class to handle a string, not an open file object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!