Насколько я понимаю, вам не нужно читать CSV-файл дважды.По сути, вам нужны все строки, где MSG
встречается в Col6
.Вы можете достичь этого в одной строке -
MSG = 'This is a test'
iter_csv = pd.read_csv('firstFile.txt', chunksize=1000, usecols=DL_fields1, skiprows=1)
# this gives you all the rows where MSG occurs in Col6
df = iter_csv.loc[iter_csv['Col6'] == MSG, :]
# this gives you all the rows where 12345 in Col1
df_12345 = df.loc[iter_csv['Col1'] == 12345,]
Таким способом вы можете создать несколько подмножеств данных.
Чтобы ответить на вторую часть вашего вопроса, вы можете выполнить циклдля всех текстовых файлов, например, так -
import glob
txt_files = glob.glob("test/*.txt")
for file in txt_files:
with open(file, 'r') as foo:
some_df = pd.read_csv(file)
РЕДАКТИРОВАТЬ: Вот как вы перебираете файлы и находите все ключи с помощью Col1=12345
и Col6=MSG
-
import glob
from functools import reduce
results_list = []
MSG = 'This is a test'
txt_files = glob.glob("test/*.txt")
for file in txt_files:
with open(file, 'r') as foo:
some_df = pd.read_csv(file, chunksize=1000, usecols=DL_fields1, skiprows=1)
df = iter_csv.loc[iter_csv['Col6'] == MSG, :]
# results_list is a list of all such dataframes
results_list.append(df.loc[iter_csv['Col1'] == 12345, ])
# All results in one big dataframe
result_df = reduce(lambda x,y: pd.concat([x,y]), results_list)