CSV比较列值python

时间:2015-09-28 06:01:25

标签: python csv

我有2个csv文件分别为 csv1 csv2

csv1

1,9,10
2,10,11
3,11,10

CSV2

2,b
1,a
3,c

我想检查第一列 csv1 的每个值到 csv2 的第一列,如果匹配则 csv2的第二列的其他值附加到 csv1 。 我的最终结果是:

1,9,10,a
2,10,11,b
3,11,10,c

3 个答案:

答案 0 :(得分:0)

使用python的CSV模块读取文件。 https://docs.python.org/2/library/csv.html

使用for循环比较两个文件并将结果写入CSV。

答案 1 :(得分:0)

<强> ALGO

  1. 使用 csv模块来读取和编写csv文件。
  2. 创建csv文件2的字典结构
  3. 以写入模式打开新文件。
  4. 以读取模式打开csv文件。
  5. 迭代csv文件1中的每一行。
  6. 检查行中的第一项是否出现在字典中(第2点)。
  7. 如果6为真,则将字典中的值附加到当前行。
  8. 将行写入文件。(第3点)。
  9. <强>码

    import csv
    # Create Dictionary structure of csv file 2
    with open("/home/vivek/2.csv", "rb") as fp:
        root = csv.reader(fp,)
        root2 = {}
        for i in root:
            root2[i[0]] = i[1]
    
    print "Debug 1: Dictionary of file 2:", root2
    
    with open("/home/vivek/output.csv", "wb") as fp:
        with open("/home/vivek/1.csv", "rb") as fp1:
            output = csv.writer(fp, delimiter=",")
            root = csv.reader(fp1,)
            for i in root:
                #Check first item from the row is present in dictionary.  
                if i[0] in root2:
                    i.append(root2[i[0]])
                output.writerow(i)
    

    列出附加Vs连接:

    >>> import timeit
    >>> def addtest():
    ...   l = []
    ...   for i in range(1000): 
    ...       l +[i]
    ... 
    >>> def appendtest():
    ...   l = []
    ...   for i in range(1000): 
    ...       l.append(i)
    ... 
    >>> print "Time 1:", timeit.timeit('appendtest()', 'from __main__ import appendtest')
    Time 1: 110.55152607
    >>> print "Time 1:", timeit.timeit('addtest()', 'from __main__ import addtest')
    Time 1: 265.882155895
    

答案 2 :(得分:0)

以下应该做你需要的,它使用Python的csv模块。它首先将整个csv2读入字典,然后可以在阅读csv1时查看密钥是否存在:

import csv  

d_csv2 = {}

with open('2.csv', 'r') as f_csv2:
    csv_2 = csv.reader(f_csv2)
    for cols in csv_2:
        d_csv2[cols[0]] = cols[1]

with open('1.csv', 'r') as f_csv1, open('output.csv', 'wb') as f_output:
    csv_1 = csv.reader(f_csv1)
    csv_output = csv.writer(f_output)

    for cols in csv_1:
        if cols[0] in d_csv2:
            csv_output.writerow(cols + [d_csv2[cols[0]]])

它会创建以下output.csv文件:

1,9,10,a
2,10,11,b
3,11,10,c
相关问题