2014-01-23 4 views
1

xml 파일을 열고 공백 문자 (줄 바꿈 제외)를 잘라내어 정규 표현식과 일치하는 모든 줄을 제거한 다음 다른 줄과 일치하는 줄을 모두 제거해야합니다. 정규식. 지금 이것은 3 개의 분리 된 임시 파일을 사용하는데, 나는 불필요한 것으로 알고있다.Python을 통해 Python을 사용하여 파일을 편집하는 방법

# 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-- 

이 모든 것을 하나의 파일로 처리 할 수있는 방법이 있습니까?

답변

1
$ sed -i -e 's/[ \r\t\f]//g' -e /pattern1/d -e /pattern2/d x.xml 

복수 -e 인수에 유의하십시오. -i 결과는 x.xml입니다.

관련 문제