简体   繁体   English

Python Writer 跳过第一行

[英]Python Writer Skips first row

I'm new to python and don't know why I get this kind of error.我是 python 的新手,不知道为什么会出现这种错误。

I have a csv file from which I read some data.我有一个 csv 文件,我从中读取了一些数据。 I compare the data with another csv file and if I find similarities I want to copy some data from the second file.我将数据与另一个 csv 文件进行比较,如果发现相似之处,我想从第二个文件中复制一些数据。 However here's the problem:然而问题来了:

            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)

In my second for loop it appears as if the first row is skipped.在我的第二个 for 循环中,它看起来好像第一行被跳过了。 Every time the for loop starts, the first row of data isn't written in the new file.每次 for 循环开始时,第一行数据都不会写入新文件。 Dataset i read from Dataset that is written Can someone tell me why the first one is always missing?我从编写的数据集中读取的数据集有人能告诉我为什么第一个总是丢失吗? If I add a dummy-row in the dataset I read from I get exactly what I want written, but I don't want to add all dummies.如果我在从中读取的数据集中添加一个虚拟行,我会得到我想要写的内容,但我不想添加所有虚拟行。

A csv reader gets 'used up' if you iterate over it.如果您对其进行迭代,csv 阅读器会“用完”。 This is why the second loop doesn't see the first row, because the first loop has already 'used' it.这就是为什么第二个循环看不到第一行的原因,因为第一个循环已经“使用”了它。 We can show this by making a simple reader over a list of terms:我们可以通过对术语列表进行简单的阅读器来展示这一点:

>>> 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)
... 
>>> 

The second time it prints nothing because the iterator has already been exhausted.第二次它什么也不打印,因为迭代器已经用尽了。 If your dataset is not too large you can solve this by storing the rows in a list, and thus in memory, instead:如果您的数据集不是太大,您可以通过将行存储在列表中来解决这个问题,因此在 memory 中,而不是:

data = [row for row in readCSV1]

If the document is too big you will need to make a second file reader and feed it to a second csv reader.如果文档太大,您需要制作第二个文件阅读器并将其提供给第二个 csv 阅读器。

The final code becomes:最终代码变为:

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 the solution to store it in memory.将其存储在 memory 中的解决方案。 If you want to use a second reader instead, it becomes如果你想使用第二个阅读器,它变成

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM