簡體   English   中英

在 For 循環中附加文件中的特定行以打開每個文件

[英]Appending Specific Lines from a File, inside For Loop to open each file

我有 4 個文件夾,里面裝滿了 txt 文檔。 我使用下面的代碼將所有 txt 和 append 提取到一個列表中。

Doc1 = glob.glob('path*.txt')
Doc2 = glob.glob('path*.txt')
Doc3 = glob.glob('path*.txt')
Doc4 = glob.glob('path*.txt')

lines = []

for file in Doc1:     ### repeated this block for Doc2, Doc3 and Doc4 ####
     f = open(file,'r')
     lines.append(f.readlines())
     f.close()

上面的代碼工作得很好。 但是,現在我想做的是:

  1. 對於文件夾中的每個 txt 文檔,我只想 append 開始和結束之間的行,以消除不必要的文本。 對於所有文件夾中的每個文檔,開始和結束文本都是相同的。 我試圖這樣做:
for file in Doc1:
     f = open(file,'r')                    #this opens the file
     for line in file:                     #for each line in the file
         tag = False                       #tag set to False, initially
        if line.startswith('text'):        #if it starts with this text then:
             tag = True                    #tag changes to True
        elif 'end text' in line:           #if it this text is in the line then:
             tag = False                   #tag stays false
             lines.append(f.readlines())   #append this line (but now tag stays False)
        elif tag:                          # if tag is true, then append that line
             lines.append(f.readlines())
    f.close()

此代碼運行,如我沒有收到任何警告或錯誤。 但是沒有線 append 到線。 TIA 尋求任何建議和幫助。

這是因為您正在使用line ,是文件,以字符串形式,未讀取,為此您可以執行以下操作:

for line in f:
   # write code here

所以你的代碼變成這樣:

import glob
Doc1 = glob.glob('path*.txt')
Doc2 = glob.glob('path*.txt')
Doc3 = glob.glob('path*.txt')
Doc4 = glob.glob('path*.txt')

lines = []

for file in Doc1:
     f = open(file,'r')                    #this opens the file
     for line in f:                     #for each line in the file
        tag = False                       #tag set to False, initially
        if line.startswith('text'):        #if it starts with this text then:
            tag = True                    #tag changes to True
        elif 'end text' in line:           #if it this text is in the line then:
            tag = False                   #tag stays false
            lines.append(f.readlines())   #append this line (but now tag stays False)
        elif tag:                          # if tag is true, then append that line
            lines.append(f.readlines())
     f.close()

該標簽在每次迭代時都會重置,這意味着它永遠不會輸出除包含end text的行之外的任何內容。 此外,還需要對操作進行一些重新排序。

lines = []
for p in Doc1:
    tag = False  # Set the initial tag to False for the new file.
    with open(p, 'r') as f:
        for line in f:
            if tag:  # First line will never be printed since it must be a start tag
                lines.append(line)
            if  line.startswith('start'):  # Start adding from next line
                tag = True
            elif 'end' in line:  # Already added the line so we can reset the tag
                tag = False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM