在python中比较匹配的列表和文件
我想循环列表中的每个元素,并检查该元素是否与文本文件中的任何产品匹配。我已经这样做了:在python中比较匹配的列表和文件,python,Python,我想循环列表中的每个元素,并检查该元素是否与文本文件中的任何产品匹配。我已经这样做了: 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', 'b
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)
嵌套的for循环仅提供productList中第一项的匹配。几天前我已经说过要学习python,所以如果有任何帮助,我们将不胜感激。第一次调用
pro\u file.readlines()
,您将看到文件的末尾。第二次你叫它的时候,就没什么可读的了
只需读取一次文件:
with open("inventory.txt", "r") as f:
pro_file = f.readlines()
然后循环pro_文件
列表
for key in productList:
for line in pro_file:
if key in line:
print("found" + key)
但是如果你只是想知道
productList = ['chips', 'biscuits', 'pasta', 'cheese', 'bread', 'rice', 'honey', 'butter', 'cake', 'salt'];
在pro_文件中
并且您不关心它在哪里,您可以通过以下方式简化上述代码:
for key in productList:
if key in pro_file:
print("found" + key)
第一次调用
pro\u file.readlines()
,您将到达文件的末尾。第二次你叫它的时候,就没什么可读的了
只需读取一次文件:
with open("inventory.txt", "r") as f:
pro_file = f.readlines()
然后循环pro_文件
列表
for key in productList:
for line in pro_file:
if key in line:
print("found" + key)
但是如果你只是想知道
productList = ['chips', 'biscuits', 'pasta', 'cheese', 'bread', 'rice', 'honey', 'butter', 'cake', 'salt'];
在pro_文件中
并且您不关心它在哪里,您可以通过以下方式简化上述代码:
for key in productList:
if key in pro_file:
print("found" + key)
问题是,一旦调用readlines(),就到达了文件的结尾,下次在同一个打开的文件上调用它时,它将不会返回任何内容。一个快速解决方法是将这两个语句交换为如下语句:
for line in pro_file.readlines():
for key in productList:
但是,对于大文件,这会有问题,因为readlines()会创建文件中所有行的列表并将其存储在内存中。所以,你可以试试这个。我添加了一些注释来解释其他一些更改
# 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')
问题是,一旦调用readlines(),就到达了文件的结尾,下次在同一个打开的文件上调用它时,它将不会返回任何内容。一个快速解决方法是将这两个语句交换为如下语句:
for line in pro_file.readlines():
for key in productList:
但是,对于大文件,这会有问题,因为readlines()会创建文件中所有行的列表并将其存储在内存中。所以,你可以试试这个。我添加了一些注释来解释其他一些更改
# 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')
您应该将
的顺序替换为循环。您应该将的顺序替换为循环。