我有几个.txt文件,我想一一打开并阅读找到字符串的完整行,然后将该字符串写入单独的.txt文件。
这是我所拥有的,但是pd.read_csv()不喜欢文件列表:
import pandas as pd
import glob
import os
file_list = glob.glob(os.path.join(os.getcwd(),folder_name,"*.txt"))
for file_path in file_list:
with open(file_path, 'w+') as file:
rowsOfLines = pd.read_csv(file_path, header=None)
for index, row in rowsOfLines.iterrows():
splitLine = row.str.split()[0]
if 'SN' in splitLine:
file.write('{} \n'.format(splitLine))
elif 'Raw_AI' in splitLine:
file.write('{} \n'.format(splitLine))
elif 'Final_AI' in splitLine:
file.write('{} \n'.format(splitLine))
elif 'ALL' in splitLine:
file.write('{} \n'.format(splitLine))
我在file_list中获得了完整路径和文件名的列表,但是pd.read_csv()不喜欢它。