我有一个带有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'语句中打开两个文件以从其中读取并写入另一个文件 –