我有两个数据集。第一个是这样的:
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列
答案 0 :(得分:0)
起初 - 您的文件有点不正确:
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:])