简体   繁体   English

比较列表和文件以在python中进行匹配

[英]Compare a list and a file for match in python

I want to loop over each element in a List and check if that element matches any of the products in the text file. 我想遍历列表中的每个元素,并检查该元素是否与文本文件中的任何产品匹配。 I have done this: 我已经做到了:

print("Opening a file")
pro_file = open("inventory.txt", "r")   #open text file for reading

#print(pro_file.read())    #read text file line by line
productList = ['chips', 'biscuits', 'pasta', 'cheese', 'bread', 'rice', 'honey', 'butter', 'cake', 'salt'];

for key in productList:
    for line in pro_file.readlines():
        if key in line:
            print("found" + key)

The nested for loops are only giving the match of first item in the productList. 嵌套的for循环仅给出productList中第一项的匹配项。 I have stated learning python few days back, so any help would be appreciated. 我说过几天前学习python,所以任何帮助将不胜感激。

The first time you call pro_file.readlines() , you reach the end of the file. 第一次调用pro_file.readlines() ,您到达文件的末尾。 The second time you call it, there is nothing more to read. 第二次调用时,没有其他内容可供阅读。

Just read the file once: 只需读取一次文件:

with open("inventory.txt", "r") as f:
    pro_file = f.readlines()

then loop the pro_file list 然后循环pro_file列表

for key in productList:
    for line in pro_file:
        if key in line:
            print("found" + key)

However if you just want to know if one of 但是,如果您只想知道其中之一

productList = ['chips', 'biscuits', 'pasta', 'cheese', 'bread', 'rice', 'honey', 'butter', 'cake', 'salt'];

is in pro_file and you don't care where it is, you can simplify the above code in the following way: pro_file ,您不必关心它在哪里,可以通过以下方式简化上述代码:

for key in productList:
    if key in pro_file:
        print("found" + key)

The problem is that once you call readlines(), the end of file is reached and the next time you call it on the same open file, it won't return anything. 问题在于,一旦您调用readlines(),文件的末尾就会到达,而下次您在同一个打开的文件上调用它时,它将不会返回任何内容。 A quick fix would be to just swap the two for statements like this: 一个快速的解决方法是将两者换成这样的语句:

for line in pro_file.readlines():
    for key in productList:

However, this will have problems with a large file since readlines() creates a list of all lines in the file and stores that in memory. 但是,这将对大文件造成问题,因为readlines()在文件中创建了所有行的列表并将其存储在内存中。 So, you could try this. 因此,您可以尝试一下。 I've added comments to explain some of the other changes. 我添加了评论以解释其他一些更改。

# Per PEP8, variable names should be all lower case with underscores between words
product_list = ['chips', 'biscuits', 'pasta', 'cheese', 'bread', 'rice', 'honey', 'butter', 'cake', 'salt'];

# This is a context manager. It will ensure the file is closed when the code block is finished
# No need to include 'r' when opening a text file as read-only. It's the default.
with open("inventory.txt") as pro_file:
    for this_line in pro_file:
        # A text file can be iterated through one line at a time this way, without loading everything into memory
        for product in product_list:
            if product in this_line:
                # Using format is cleaner and easier to read than + and will work even if the value is a number
                print('Found: {}'.format(product))
                # Since you found a product, you can stop checking products. Break will stop the for product loop.
                break
        else:  # no break
            # What the heck is this? An else in a for loop?
            # In Python, this means "do this if you didn't hit a break statement," and it's very useful.
            # I usually add the comment "no break" after it so it's a little more clear that it was intentional.
            print('Did not find any products in this line')

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

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