Python 3.5 通过匹配多列合并两个CSV
Python 3.5 combine two CSV by matching multiple columns
我有两个数据集。第一个是这样的:
data file:
Column 1, Column 2, Column 3, Column 4, Column 5, Column 6
1111111, 2222222, 3333333, 44444444, 55555555, 666666666
0000000, 77777777, 8888888, 99999999, 10101010, 121212121
3333333, 55555555, 9999999, 88888888, 22222222, 111111111
第二个文件是这样的:
descriptors file:
Column 1, Column 2, Column 3
11111111,, this is a descriptor
,777777777, this is a descriptor again
99999999, , last descriptor
我想要的是:
Column 1, Column 2, Column 3, Column 4, Column 5, Column 6, Column 7
1111111, 2222222, 3333333, 44444444, 55555555, 666666666, this is a descriptor
0000000, 77777777, 8888888, 99999999, 10101010, 121212121, this is a descriptor again
3333333, 55555555, 9999999, 88888888, 22222222, 111111111
我有以下代码,来自我操纵的论坛:
import csv
with open('descriptors file.CSV', 'r') as first_file:
reader = csv.reader(first_file)
first_header = next(reader, None)
file_information = {row[0]: row for row in reader}
with open('data file.CSV', 'r') as second_file:
with open('final results.csv', 'w', newline='') as outfile:
reader = csv.reader(second_file)
second_header = next(reader, None)
writer = csv.writer(outfile)
writer.writerow(second_header[:6] + first_header[2:])
for row in reader:
if row[0] not in file_information:
continue
newrow = row[0:] + file_information[row[0]]
writer.writerow(newrow)
我的问题如下:
1).我想在第 0 列和第 1 列(1 和 2)之间进行匹配;我不在 2 列之间进行匹配;只有一个
2).结果不包括空行。例如,如果在描述符文件中找不到与数据文件匹配的任何内容,我宁愿将数据保留在数据文件中而不是将其丢弃。数据文件应该由描述符文件增加,而不是减少。
3).我不知道如何只写描述符列,而不是描述符文件中的整个 3 列
起初 - 您的文件有点不正确:
1111111 != 11111111
77777777 != 777777777
我已经解决了这个问题,这段代码对我来说效果很好。抱歉硬编码。如果您需要更复杂的解决方案 - 请说明您的实际需求。
import csv
with open('d_file.csv', 'r') as first_file:
reader = csv.reader(first_file)
first_header = next(reader, None)
column0= {}
column1 = {}
for row in reader:
if row[0]:
column0[row[0].strip()] = row[2]
if row[1]:
column1[row[1].strip()] = row[2]
with open('data_file.csv', 'r') as second_file:
with open('final_results.csv', 'w', newline='') as outfile:
reader = csv.reader(second_file)
second_header = next(reader, None)
description = len(second_header)-1
writer = csv.writer(outfile)
# use there first_header[2:] is incorrect - you will save 'Column 3', while you want 'Column 7'
writer.writerow(second_header[:6] + ['Column 7'])
for row in reader:
if row[0].strip() in column0:
writer.writerow(row[0:] + [column0[row[0].strip()]] )
elif row[1].strip() in column1:
writer.writerow(row[0:] + [column1[row[1].strip()]] )
else:
writer.writerow(row[0:])
我有两个数据集。第一个是这样的:
data file:
Column 1, Column 2, Column 3, Column 4, Column 5, Column 6
1111111, 2222222, 3333333, 44444444, 55555555, 666666666
0000000, 77777777, 8888888, 99999999, 10101010, 121212121
3333333, 55555555, 9999999, 88888888, 22222222, 111111111
第二个文件是这样的:
descriptors file:
Column 1, Column 2, Column 3
11111111,, this is a descriptor
,777777777, this is a descriptor again
99999999, , last descriptor
我想要的是:
Column 1, Column 2, Column 3, Column 4, Column 5, Column 6, Column 7
1111111, 2222222, 3333333, 44444444, 55555555, 666666666, this is a descriptor
0000000, 77777777, 8888888, 99999999, 10101010, 121212121, this is a descriptor again
3333333, 55555555, 9999999, 88888888, 22222222, 111111111
我有以下代码,来自我操纵的论坛:
import csv
with open('descriptors file.CSV', 'r') as first_file:
reader = csv.reader(first_file)
first_header = next(reader, None)
file_information = {row[0]: row for row in reader}
with open('data file.CSV', 'r') as second_file:
with open('final results.csv', 'w', newline='') as outfile:
reader = csv.reader(second_file)
second_header = next(reader, None)
writer = csv.writer(outfile)
writer.writerow(second_header[:6] + first_header[2:])
for row in reader:
if row[0] not in file_information:
continue
newrow = row[0:] + file_information[row[0]]
writer.writerow(newrow)
我的问题如下:
1).我想在第 0 列和第 1 列(1 和 2)之间进行匹配;我不在 2 列之间进行匹配;只有一个
2).结果不包括空行。例如,如果在描述符文件中找不到与数据文件匹配的任何内容,我宁愿将数据保留在数据文件中而不是将其丢弃。数据文件应该由描述符文件增加,而不是减少。
3).我不知道如何只写描述符列,而不是描述符文件中的整个 3 列
起初 - 您的文件有点不正确:
1111111 != 11111111
77777777 != 777777777
我已经解决了这个问题,这段代码对我来说效果很好。抱歉硬编码。如果您需要更复杂的解决方案 - 请说明您的实际需求。
import csv
with open('d_file.csv', 'r') as first_file:
reader = csv.reader(first_file)
first_header = next(reader, None)
column0= {}
column1 = {}
for row in reader:
if row[0]:
column0[row[0].strip()] = row[2]
if row[1]:
column1[row[1].strip()] = row[2]
with open('data_file.csv', 'r') as second_file:
with open('final_results.csv', 'w', newline='') as outfile:
reader = csv.reader(second_file)
second_header = next(reader, None)
description = len(second_header)-1
writer = csv.writer(outfile)
# use there first_header[2:] is incorrect - you will save 'Column 3', while you want 'Column 7'
writer.writerow(second_header[:6] + ['Column 7'])
for row in reader:
if row[0].strip() in column0:
writer.writerow(row[0:] + [column0[row[0].strip()]] )
elif row[1].strip() in column1:
writer.writerow(row[0:] + [column1[row[1].strip()]] )
else:
writer.writerow(row[0:])