我必须打开一个 xml 文件,修剪它的空格(换行符除外),删除与正则表达式匹配的所有行,然后删除与另一个正则表达式匹配的所有行。现在这是使用 3 个单独的临时文件,我知道这是不必要的。
# Trim whitespace from xml
f2 = open(fname + '.xml','r')
f3 = open(fname + 'temp.xml', 'w')
subprocess.call(["tr", "-d", "'\t\r\f'"], stdin=f2, stdout=f3)
f2.flush()
f3.flush()
# Remove the page numbers from the file
f4 = open(fname + 'temp2.xml', 'w')
subprocess.call(["sed",
"/<attr key=\"phc.line_number\"><integer>[0-9]*<\/integer><\/attr>/d",
fname + 'temp.xml'], stdout=f4)
f4.flush()
# Remove references to filename from the file
--not implemented--
有没有办法让我用一个文件完成所有这些工作?