첫 번째를 제외하고 특정 문자를 포함하는 파일의 모든 줄을 제거하는 방법은 무엇입니까?

sequence_hard

디렉토리의 모든 파일을 병합 한 다음 출력 파일에서 원하지 않는 줄을 제거하는 스크립트를 만들려고합니다. 제거하려는 행에는 동일한 문자열 패턴이 포함되어 있으며 첫 번째 행 (파일의 첫 번째 행이기도 함)을 제외한 모든 행을 제거하고 싶습니다. 내가 그것을 시도하는 방법은 다음과 같습니다.

import glob

# Merge all output files into one file
read_files = glob.glob('/home/user/Results/Script_tests/TestResults/*.output')

with open('MergedOutput.txt', 'r+b') as outfile:
    for file in read_files:
        with open(file, 'r+b') as infile:
            outfile.write(infile.read())

print 'Files merged.'          

# Remove header rows except from row 1

final_output = open('FinalMergedOutput.txt', 'r+b')
with open('MergedOutput.txt', 'r+b') as file:
    for line in file:
        if line == 0 and line.startswith('File'):
            final_output.write(line)
        elif line > 0 and not line.startswith('File'):
             final_output.write(line) 

print 'Headers removed except on line 1.' 

병합 부분은 일부 줄이 FinalMergedOutput.txt. 그러나 줄을 제거하면로 시작하는 모든 줄이 제거 File되고 첫 번째 줄을 아끼지 않습니다.

누구든지 이것에 대한 우아한 해결책이 있습니까?

보행

for line in file줄 번호가 아니라 파일의 실제 내용을 반복합니다. 빈 문자열조차도 0보다 크므로 첫 번째 조건은 결코 참일 수 없으며 두 번째 조건은 항상 .startswith(..)입니다 (when is also true ...).

목록의 첫 번째 항목을 특수하게 처리하기위한 to의 많은 관용구가 있습니다. 이것은 코드를 최소한으로 조정하는 매우 간단합니다.

for line_num,line in enumerate(file):
    if line_num == 0 and line.startswith('File'):
        final_output.write(line)
    elif line_num > 0 and not line.startswith('File'):
         final_output.write(line) 

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관