我不久前开始使用python,我再次需要您的帮助,我有一个包含缓存数据的csv文件,并且我使用for来通过数据过滤器并将过滤后的数据保存在数组中作为示例
filters = ['LMS', 'atx', 'arx-dsd']
search_result = []
cached_file = open("teste.csv", "r")
search_result.append(cached_file.readline())
for words in filters:
print(words)
if_find = [x for x in cached_file if words in x]
print(if_find)
if if_find:
search_result.extend(if_find)
输出:
LMS
[us-east-1a,windows,running,x86_64,IBM,LMS]
ATX
[]
arx-dsd
[]
找不到其余结果,只是数组中的第一个,如果您进行单独的搜索,它将找到所有结果
我相信我的lambda错误,所以结果错误
最佳答案
@stovfl已经为您提供了解决方案:您不能多次从file object中读取信息,
要解决此问题,您可以将文件行存储在变量中:
with open("teste.csv", "r") as f:
cached_file = f.readlines()