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--
有沒有辦法讓我用一個文件做所有這一切?