簡體   English   中英

Python:讀取多個源txt文件,按條件復制到1個輸出文件中

[英]Python: read multiple source txt files, copy by criteria into 1 output file

我的目標是讀取一個文件夾(較小)中的多個txt源文件,然后將按條件選擇的行復制到一個輸出txt文件中。 我可以使用1個源文件執行此操作,但是當我嘗試讀取多個文件並執行相同操作時卻沒有輸出(空)。

通過我的SO研究,我編寫了以下代碼(無輸出):

import glob
# import re  --- taken out as 'overkill'

path = 'C:/Doc/version 1/Input*.txt'   # read source files in this folder with this name format
list_of_files=glob.glob(path)   

criteria = ['AB', 'CD', 'EF']   # select lines that start with criteria

#list_of_files = glob.glob('./Input*.txt')

with open("P_out.txt", "a") as f_out:
    for fileName in list_of_files:
        data_list = open( fileName, "r" ).readlines()
    for line in data_list:
        for letter in criteria:
            if line.startswith(letter): 
                f_out.write('{}\n'.format(line))

謝謝您的幫助。

@abe和@ppperry:非常感謝您之前的輸入。

您的代碼有問題:

  1. 您有兩個重復的變量fileslist_of_files但僅使用后者。
  2. 每次打開文件時,都將覆蓋變量data_list ,這會擦除先前讀取的文件的內容。
  3. 在文件中搜索匹配data_list ,請使用變量fileName而不是data_list

可以簡化的地方:

  1. 僅使用re模塊只是為了確定一個字符串是否以另一個字符串開頭,所以使用re模塊是多余的。 您可以使用line.startswith(letter)

錯誤:

  1. 第14行應在data_list中查找行,而不是fileName。
  2. “我可以使用1個源文件來執行此操作,但是當我嘗試讀取多個文件並執行相同操作時,我沒有輸出(空)。” 第14到17行應該縮進,否則迭代list_of_files的for循環將僅循環第一個文件。
  3. 您甚至沒有使用第4行和第5行,那么為什么要包含它們? 它們沒有作用。

這是固定的代碼,帶有注釋:

import glob
import re

#path = 'C:\Doc\version 1\Output*.txt'   # read all source files with this name format
#files=glob.glob(path)

criteria = ['AB', 'CD', 'EF']   # select lines that start with criteria

list_of_files = glob.glob('./Output*.txt')

with open("P_out.txt", "a") as f_out: #use "a" so you can keep the data from the last Output.txt
    for fileName in list_of_files:
        data_list = open( fileName, "r" ).readlines()
        #indenting the below will allow you to search through all files.
        for line in data_list: #Search data_list, not fileName
            for letter in criteria:
                if re.search(letter,line):
                    f_out.writelines('{}\n'.format(line))
                    #I recommend the \n so that the text does not get concatenated when moving from file to file. 

#Really? I promise with will not lie to you. 
#f_out.close()  # 'with' construction should close files, yet I make sure they close

對於那些反對的人,為什么不添加評論以證明您的判斷正確呢? OP要求的所有條件都已滿足。 如果您認為可以進一步改善答案,請提出修改建議。 謝謝。

暫無
暫無

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

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