1
我必须打开一个xml文件,修剪它的空白(除了换行符),删除所有与正则表达式匹配的行,然后删除所有与其他行匹配的行正则表达式。现在这是使用3个单独的临时文件,我知道这是不必要的。如何通过Python运行unix命令来编辑文件
# 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--
有没有办法让我用一个文件做所有这一切?