[英]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:非常感謝您之前的輸入。
您的代碼有問題:
files
和list_of_files
但僅使用后者。 data_list
,這會擦除先前讀取的文件的內容。 data_list
,請使用變量fileName
而不是data_list
! 可以簡化的地方:
re
模塊只是為了確定一個字符串是否以另一個字符串開頭,所以使用re
模塊是多余的。 您可以使用line.startswith(letter)
。 錯誤:
這是固定的代碼,帶有注釋:
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.