我有一個帶有EPA信息的文本文件,它們是按狀態編碼的。我想把每個狀態分解成它自己的文本文件。這裏是我正在使用的代碼:解析一個文本文件並尋找一個特定的字符串
from __future__ import print_function
import os, sys
import numpy as np
print(os.getcwd())
lines = [] #lines from file
with open('UCMR3_All.txt') as well_list:
for line in well_list:
if line == "AL":
#what goes here?
well_list_output = open(os.path.join('..','well_list_output.txt'),'w')
for line in lines:
well_list_output.write(line)
well_list_output.close()
基本上,我想要一個包含「AL」的行並將其輸出到它自己的文件。我試圖使用lines.append(line)
,但似乎沒有幫助。我肯定會接受有用的推動或指導來代替答案!
當然這個問題的網站超過10倍。 – AK47
用於file1中的行:if'AL'in line:file2.write(line)?? – AK47
您可以在一個'with'語句中打開兩個文件以從其中讀取並寫入另一個文件 –