我需要匹配來自多個CSV文件的數據。我編寫了一個簡單數據的腳本,但分析4000行的速度很慢。 我已經使用set(a) & set(b)
進行了嘗試,但無法從每個文件中返回匹配數據。 輸出文件必須具有來自所有文件的匹配數據。Python:使用來自多個CSV文件的匹配數據寫入CSV
腳本:
for file_1 in files:
with open(file_1, 'rt') as f1,open(saved_file, 'w') as f3:
reader1 = csv.reader(f1, delimiter = ';')
writer = csv.writer(f3, delimiter = ';', lineterminator = '\n')
for row1 in reader1:
for row1 in reader1:
for file_2 in files:
with open(file_2, 'rt') as f2:
reader2 = csv.reader(f2, delimiter = ';')
if row1 in reader2:
writer.writerow(row1)
我想匹配的數據是這樣的:
File_1:
May 22, 2017;12,615.50;12,650.50;12,665.00;12,567.00;-;-0.18%
May 19, 2017;12,638.69;12,612.30;12,658.55;12,596.72;121.95M;0.39%
May 18, 2017;12,590.06;12,608.19;12,634.26;12,489.95;123.48M;-0.33%
May 17, 2017;12,631.61;12,700.12;12,786.89;12,587.45;108.95M;-1.35%
May 15, 2017;12,807.04;12,824.05;12,832.29;12,729.49;87.08M;0.29%
File_2:
May 22, 2017;1.1238;1.1200;1.1265;1.1160;0.28%
May 19, 2017;1.1207;1.1100;1.1214;1.1094;0.94%
May 17, 2017;1.1159;1.1082;1.1163;1.1078;0.69%
May 16, 2017;1.1082;1.0975;1.1098;1.0971;0.97%
May 15, 2017;1.0975;1.0924;1.0991;1.0920;0.40%
輸出: 保存d_file_1:
May 22, 2017;12,615.50;12,650.50;12,665.00;12,567.00;-;-0.18%
May 19, 2017;12,638.69;12,612.30;12,658.55;12,596.72;121.95M;0.39%
May 17, 2017;12,631.61;12,700.12;12,786.89;12,587.45;108.95M;-1.35%
May 15, 2017;12,807.04;12,824.05;12,832.29;12,729.49;87.08M;0.29%
saved_file_2:
May 22, 2017;1.1238;1.1200;1.1265;1.1160;0.28%
May 19, 2017;1.1207;1.1100;1.1214;1.1094;0.94%
May 17, 2017;1.1159;1.1082;1.1163;1.1078;0.69%
May 15, 2017;1.0975;1.0924;1.0991;1.0920;0.40%
它適用於2個文件,但我需要搜索文件列表並匹配每個文件。 –
請參閱編輯。 –