首页 文章

CSV比较列值python

提问于
浏览
0

我有2个csv文件分别为 csv1csv2 .

csv1

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

csv2

2,b
1,a
3,c

我想检查 csv1 的第1列的每个值到 csv2 的第1列,如果匹配, csv2 的第2列的其他值被追加到 csv1 . 我的最终输出是:

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

3 回答

  • 0

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

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

  • 0

    Algo

    • 使用 csv module 来读取和编写csv文件 .

    • 创建csv文件的字典结构2

    • 以写入模式打开新文件 .

    • 在读取模式下打开csv文件1 .

    • 迭代csv文件1中的每一行 .

    • 检查行中的第一项是否存在于字典中(第2点) .

    • 如果6为真,则将字典中的值附加到当前行 .

    • 将行写入文件 . (第3点) .

    code

    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)
    

    清单 Append Vs Concatenation:

    >>> 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
    
  • 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
    

相关问题