Python 作者跳过第一行

Python Writer Skips first row

我是 python 的新手,不知道为什么会出现这种错误。

我有一个 csv 文件,我从中读取了一些数据。我将数据与另一个 csv 文件进行比较,如果发现相似之处,我想从第二个文件中复制一些数据。然而问题来了:

            with open('WeselVorlageRE5Woche.csv') as woche:
                with open('weselfund.csv','+a',newline='') as fund:

                    readCSV1 = csv.reader(woche, delimiter=';')
                    for row1 in readCSV1:   
                        if row[1]==row1[4]: #find starting time
                            if row[3]==row1[1]: # find same train
                                if row[2]=='cancelled': # condition for taking row
                                    zug=row1[6]     #copy trainnumber
                                    print(zug)
                                    for row2 in readCSV1:
                                        if row2[6]==zug: #find all trainnumbers
                                            #write data to csv
                                            writer = csv.writer(fund, delimiter=';')
                                            writer.writerow(row2)

在我的第二个 for 循环中,它似乎跳过了第一行。每次 for 循环开始时,第一行数据都不会写入新文件。 Dataset i read from Dataset that is written 谁能告诉我为什么第一个总是不见了? 如果我在读取的数据集中添加一个虚拟行,我就会得到我想要写的内容,但我不想添加所有虚拟行。

如果您遍历 csv reader,它会得到 'used up'。这就是为什么第二个循环看不到第一行的原因,因为第一个循环已经 'used' 它了。我们可以通过对术语列表进行简单的 reader 来证明这一点:

>>> import csv
>>> test = ["foo", "bar", "baz"]
>>> reader = csv.reader(test)
>>> for row in reader:
...     print(row)
... 
['foo']
['bar']
['baz']
>>> for row in reader:
...     print(row)
... 
>>> 

第二次它什么也不打印,因为迭代器已经用完了。如果你的数据集不是太大,你可以通过将行存储在列表中来解决这个问题,因此在内存中,而不是:

data = [row for row in readCSV1]

如果文档太大,您需要制作第二个文件 reader 并将其提供给第二个 csv reader.

最终代码变为:

with open('WeselVorlageRE5Woche.csv') as woche:
    with open('weselfund.csv','+a',newline='') as fund:
        readCSV1 = [row for row in csv.reader(woche, delimiter=';')]
        for row1 in readCSV1:   
            if row[1]==row1[4]: #find starting time
                if row[3]==row1[1]: # find same train
                    if row[2]=='cancelled': # condition for taking row
                        zug=row1[6]     #copy trainnumber
                        print(zug)
                        for row2 in readCSV1:
                            if row2[6]==zug: #find all trainnumbers
                                #write data to csv
                                writer = csv.writer(fund, delimiter=';')
                                writer.writerow(row2)

将解决方案存储在内存中。如果你想用第二个 reader 代替,它变成

with open('WeselVorlageRE5Woche.csv') as woche:
    with open('weselfund.csv','+a',newline='') as fund:
        readCSV1 = [row for row in csv.reader(woche, delimiter=';')]
        for row1 in readCSV1:   
            if row[1]==row1[4]: #find starting time
                if row[3]==row1[1]: # find same train
                    if row[2]=='cancelled': # condition for taking row
                        zug=row1[6]     #copy trainnumber
                        print(zug)
                        with open('WeselVorlageRE5Woche.csv') as woche2:
                            readCSV2 = csv.reader(woche2, delimiter=';')
                            for row2 in readCSV2:
                                if row2[6]==zug: #find all trainnumbers
                                    #write data to csv
                                    writer = csv.writer(fund, delimiter=';')
                                    writer.writerow(row2)