如何比较两个csv文件?

时间:2017-12-06 00:37:22

标签: python csv

我有 csv file1 ,就像

FLAGSTAFF   AZ  50244.67    5.02    KA1_Podium_Garage_S
FLAGSTAFF   AZ  33752.13    3.38    KA1_Podium_Garage_S
FLAGSTAFF   AZ  11965.5      1.2    KA1_Podium_Garage_S
FLAGSTAFF   AZ  3966.48      0.4    KA1_Podium_Garage_S
SEATTLE     WA  12646.9      1.26   KA1_Podium_Garage_S
SEATTLE     WA  225053.92   22.51   KA1_Podium_Garage_S
SEATTLE     WA  23974.3      2.4    KA1_Podium_Garage_S
SEATTLE     WA  7036.4       0.7    KA1_Podium_Garage_S
SEATTLE     WA  3021.93      0.3    KA1_Podium_Garage_S

我有一个csv文件2,就像,

Alabama     AL  1
Alaska      AK  2
Arizona     AZ  4
Arkansas    AR  5
California  CA  6
Colorado    CO  8
Connecticut CT  9
SEATTLE     WA  53

现在我必须将csv file2中的第三列值附加到csv file1, 通过比较第二列

例如它应该是,

AZ代码是4 WA代码是53 无论AZ,WA在我的csv file1中,代码都应该附加到列

我的输出应该是,

FLAGSTAFF   AZ  50244.67    5.02    KA1_Podium_Garage_S 4
FLAGSTAFF   AZ  33752.13    3.38    KA1_Podium_Garage_S 4
FLAGSTAFF   AZ  11965.5      1.2    KA1_Podium_Garage_S 4
FLAGSTAFF   AZ  3966.48      0.4    KA1_Podium_Garage_S 4
SEATTLE     WA  12646.9      1.26   KA1_Podium_Garage_S 53
SEATTLE     WA  225053.92   22.51   KA1_Podium_Garage_S 53
SEATTLE     WA  23974.3      2.4    KA1_Podium_Garage_S 53
SEATTLE     WA  7036.4       0.7    KA1_Podium_Garage_S 53
SEATTLE     WA  3021.93      0.3    KA1_Podium_Garage_S 53

这是我试过的代码,

with open("/home/sumit/Desktop/CSV_FILE1.csv", "r") as f:
    first = {rows[1]: rows[0:] for rows in list(csv.reader(f))}

# compare second csv and append Code
with open("CSVFILE2.csv", "r") as f1:
    for row in csv.reader(f1):
        if row[1] in first:
            first[row[1]].append(row[2])

# convert dict back to list
merged = [(k,) + tuple(v) for k, v in first.items()]

# write list to output csv
with open("output.csv", "w") as f1:
    csv.writer(f1).writerows(merged)

输出结束,

AZ  FLAGSTAFF   AZ  44230.4 4.42    KA1_Podium_Garage_S 4
WA  SEATTLE     WA  45329.3 4.53    KA1_Podium_Garage_S 53

1 个答案:

答案 0 :(得分:2)

此行正在根据您的第一个文件创建一个基于州缩写的字典。这看起来不对,因为字典只能有一次每个键,并且你有多行" AZ"例如。

return this.http.post(url, message, {
   headers: new HttpHeaders().set('Content-Type', 'application/json')
                             .set('Accept', 'application/json')
                             .set('Access-Control-Allow-Headers', 'Content-Type')
});

相反,您应该根据第二个文件创建词典:

first = { rows[1]: rows[0:] for rows in list(csv.reader(f)) }

然后,循环遍历第一个文件,并将相应的代码附加到每一行。

with open("CSVFILE2.csv", "r") as f1:
    code = { row[1]: row[2] for row in csv.reader(f1) }

将合并后的数据写出来。

with open("/home/sumit/Desktop/CSV_FILE1.csv", "r") as f: merged = [ row + [code[row[1]]] for row in csv.reader(f) ] 的解释:

如果行是row + [code[row[1]]],那么

  • ["FLAGSTAFF", "AZ", 50244.67, 5.02, "KA1_Podium_Garage_S"]row[1]
  • "AZ"code[row[1]]code["AZ"]
  • 4是列表[code[row[1]]]
  • [4]是列表并置,生成所需的新行值:row + [code[row[1]]