如何检查字符串列表中的多个单词?

问题描述

我有一个包含多个文件文件夹,而我正在寻找一个仅包含“低”和“ 1小时”的文件。所以要读取所有这些文件地址,我正在使用glob库。

import glob
path = glob.glob("summary output\*.xlsx")
print(path)

输出

summary output\2030 and 2030 High RE with 1 Hour storage.xlsx
summary output\2030 and 2030 High RE with 2 Hour storage.xlsx
summary output\2030 and 2030 High RE with 4 Hour storage.xlsx
summary output\2030 and 2030 High RE with 6 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 1 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 2 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 4 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 6 Hour storage.xlsx

这些是该summary output文件夹中文件的列表。现在我想要一个包含Low1 Hour文件,基本上是这个文件

summary output\2030 and 2030 Low RE with 1 Hour storage.xlsx

为此,我写了这个:

for file in path:
    if "Low"  and "1 Hour" in file:
        print(file)

输出

summary output\2030 and 2030 High RE with 1 Hour storage.xlsx
summary output\2030 and 2030 Low RE with 1 Hour storage.xlsx

我不知道为什么要使用第一个文件,所以任何人都可以帮忙吗?

解决方法

尝试一下

for file in path:
if "Low"  in file and "1 Hour" in file:
    print(file)
,

无可奉告,所以这还是我的2美分。

尝试

If "Low" in file and "1 Hour" in file:
     Print file

现在,您的病情正在发挥作用-

"Low" and ("1 Hour" in file)

也请检查此-Change the TextInputLayout outline color

另一方面,您可以让通配符为您完成工作-

glob.glob("summary output\*Low*1*.xlsx")
,
for file in path:
    #The below line is not checking for 'Low' substring in path,but checking if "Low" is equal to None,so it always returns True.
    if "Low"  and "1 Hour" in file:  
        print(file)