首页 > 解决方案 > Python Writer 跳过第一行

问题描述

我是 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 循环开始时,第一行数据都不会写入新文件。 我从编写的数据集中读取的 数据集 有人能告诉我为什么第一个总是丢失吗?如果我在从中读取的数据集中添加一个虚拟行,我会得到我想要写的内容,但我不想添加所有虚拟行。

标签: pythonfor-loopexport-to-csv

解决方案


如果您对其进行迭代,则 csv 阅读器会“用完”。这就是为什么第二个循环看不到第一行的原因,因为第一个循环已经“使用”了它。我们可以通过对术语列表进行简单的阅读器来展示这一点:

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

如果文档太大,您将需要制作第二个文件阅读器并将其提供给第二个 csv 阅读器。

最终代码变为:

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)

将其存储在内存中的解决方案。如果你想使用第二个阅读器,它变成

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)

推荐阅读