Python编写器跳过第一行

2024-06-26 11:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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 fromDataset that is written 有人能告诉我为什么第一个总是不见了吗? 如果我在从中读取的数据集中添加一个虚拟行,我会得到我想要写的内容,但我不想添加所有的虚拟行


Tags: 文件csv数据forifaswithopen
1条回答
网友
1楼 · 发布于 2024-06-26 11:17:37

如果您对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)

相关问题 更多 >