如何检查csv

时间:2016-11-28 23:21:03

标签: python-2.7 csv

我正在尝试从旧文件中创建一个组合的csv文件,这些文件有一些重复,但我的代码中有些错误。 'test2.csv'更大,有新行,'test1.csv'是我想要制作的新文件。

new = []
with open('test1.csv','rb') as file1:
    reader = csv.reader(file1,delimiter=',' )
    with open('test2.csv','rb') as file2:
        reader2 = csv.reader(file2,delimiter=',')
        for row in reader2:
            if row not in reader:
                new.append(row)

for row in new:
    print row

编辑:

我的测试文件看起来像这样 test1.csv:

28/11 16    VwS (10)    Ha MaSSe (16)   mirage  Global Offensive Champions League Season 5  39247   6756    7472    2596    10  16  8459,9434,11307,9410,8460   11838,11837,12943,11840,12944
28/11 16    Ulti (0)    Signature (16)  mirage  fpsGOD Super League Winter  39251   7343    6774    2533    0   16  12429,12427,12430,12428,12431   9288,10664,9289,11248,12129

test2.csv:

28/11 16    Singularity (14)    Elements (16)   nuke    Global Offensive Champions League Season 5  39252   6978    7489    2596    14  16  9317,2822,8862,8875,7463    8726,7405,8727,8368,8410
28/11 16    eSuba (9)   Pathless (16)   cache   Gauntlet: Fight for the Crown   39253   5412    7433    2430    9   16  9666,10458,7723,9358,3143   9891,9412,12767,12945,12766
28/11 16    Pathless (16)   eSuba (13)  mirage  Gauntlet: Fight for the Crown   39248   7433    5412    2430    16  13  9412,12945,12767,9891,12766 9666,7723,10458,9961,9358,3143
28/11 16    VwS (10)    Ha MaSSe (16)   mirage  Global Offensive Champions League Season 5  39247   6756    7472    2596    10  16  8459,9434,11307,9410,8460   11838,11837,12943,11840,12944
28/11 16    Ulti (0)    Signature (16)  mirage  fpsGOD Super League Winter  39251   7343    6774    2533    0   16  12429,12427,12430,12428,12431   9288,10664,9289,11248,12129

所以,我正在尝试将缺少的行添加到test2.csv的test1.csv中。

1 个答案:

答案 0 :(得分:0)

您的代码段中未修改文件test1.csv。使用csv.reader(file1,delimiter=',')创建阅读器后,reader之后根本不会更新。

在你的循环中修改new,所以检查你的行是否在那里。 如果您不想打印出test1.csv中已有的行,则需要读取它们并在迭代第二个文件中的行时检查它们是否不重复。

with open('test1.csv','rb') as file1:
    existingLines = [line for line in csv.reader(file1, delimiter=',')]

new = []
with open('test2.csv','rb') as file2:
    reader2 = csv.reader(file2,delimiter=',')
    for row in reader2:
        if row not in new and row not in existingLines:
            new.append(row)

for row in new:
    print row