Python - 在嵌套 for-loop 的一个 CSV 文件中搜索字符串

Python - Search strings in one CSV file with nested for-loop

我是 Whosebug 的新手,正在学习 python。

我在使用 Python.

搜索一个 CSV 文件中是否有多个字符串(从输入文件中获取)时遇到一些问题

基本上,我的 python 代码从一个输入文件 (inputfile.csv) 中一个接一个地获取字符串,并搜索每个字符串是否在另一个名为 [=82= 的文件的第一列中].它只与 mainfile.csv 的第一列进行比较,其中包含我要查找的相关数据。

注意 :文件非常大,mainfile.csv 超过 100 万行(并且还在增长),inputfile.csv 通常大约 30,000 行。

这是代码。

#!/usr/bin/python
import csv

mainfile = open('mainfile.csv', 'rb')
inputfile = open('inputfile.csv', 'rb')

mfreader = csv.reader(mainfile, delimiter=',') # mainfile reader
ifreader = csv.reader(inputfile) # inputfile reader, just one column, no delimeter

for ifrow in ifreader:
    for mfrow in mfreader:
        if ifrow[0] == mfrow[0]:
            print ifrow[0], mfrow[0] # This line is a print for debugging purpose
            print "Found a match for : %s " % ifrow[0]
            perform_some_operations()
        else:
            print ifrow[0], mfrow[0] # This line is a print for debugging purpose
            continue

mainfile.close()
inputfile.close()

问题: "nested for-loop" 仅在 inpufile 的第一行中执行。它"ignores"其他行inputfile.csv。

EDIT

In fact my comprehension of the problem was wrong. The first for-loop do steps through all the rows of the inputfile. This is the second nested for-loop which only goes once through the iteration process. And since it reaches the end, it doesn't perform any more iteration when the first for-loop iterates.

示例文件

下面是一些示例文件。为了示例的目的,"rows values" 被简化了。

基本上,我们处理 2 个文件:

MainFile(mainfile.csv,文件大小:> 1,000,000 (1M) 行)

类型:序列号[varchar(64)]、型号[(varchar(64)]、信息[varchar(2048)]

SerialNumber, ModelNumber, Informations
SN111aaa, MN123425, Informations for SN111aaa
SN222bbb, MN123425, Informations for SN222bbb
SN333ccc, MN456789, Informations for SN333ccc
SN444ddd, MN654321, Informations for SN444ddd
SN555eee, MN123425, Informations for SN555eee

InputFile(inputfile.csv,文件大小:~30,000(30K)行)

类型:序列号[varchar(64)]

SN000xyz
SN111xyz
SN222xyz
SN333xyz
SN444ddd

在上面的示例中,由于 SN444ddd 是唯一可以在输入文件和主文件中找到的字符串,因此我的 python 代码应该 return 我(如果我们关闭调试行):

Found a match for SN444ddd

然后我可以执行一些操作。

但事实并非如此。我从调试打印行中得到的是:

$ ./myprogram.py
SN000xyz SerialNumber
SN000xyz SN111aaa
SN000xyz SN222bbb
SN000xyz SN333ccc
SN000xyz SN444ddd
SN000xyz SN555eee
$

仅处理输入文件的第一行。

EDIT WRONG. cf. previous edit.

它也与mainfile.csv header进行比较,但是"issue"并不是很重要。

我哪里弄错了?

感谢您的帮助。

主要问题似乎是ifreadermfreaderiterators,这意味着一旦他们用完可用项目列表,他们将不会重新开始。

第二个问题是您的方法效率很低。我建议不要在内部循环中一次又一次地通过迭代器,而是从 inputfile.csv 制作一个 set 的序列号。集合不能包含重复值,并且它们对于检查值是否存在非常有效。

因此您的代码可能如下所示:

#!/usr/bin/python
import csv

def perform_some_operations():
    # ...
    pass

with open('inputfile.csv', 'rb') as inputfile:
    ifreader = csv.reader(inputfile) # inputfile reader, just one column, no delimeter
    serial_numbers = {row[0] for row in ifreader}

with open('mainfile.csv', 'rb') as mainfile:
    mfreader = csv.reader(mainfile, delimiter=',') # mainfile reader

    for row in mfreader:
        if row[0] in serial_numbers:
            print "match for    : %s " % row[0]
            perform_some_operations()
        else:
            print "NO MATCH for : %s " % row[0]

这里我使用了 集合理解 (大括号)来用 ifreader 中的值填充集合。之后,可以使用 in 运算符轻松检查集合中的特定值。


注意 - 不要使用 'rb' 模式来读取文件,您真的应该使用 codings 模块并在打开文件时指定文件编码。

import codecs

with codecs.open('inputfile.csv', 'r', encoding='utf8') as inputfile:
    ...

使用与您的源数据匹配的正确 encoding 参数。在 Python 3 中,open() 函数本身支持 encoding 参数,在 Python 2 中,模块可以提供帮助。