gpt4 book ai didi

python - 如何检查一行是否包含列表中的字符串并打印匹配的字符串

转载 作者:行者123 更新时间:2023-11-28 20:56:09 24 4
gpt4 key购买 nike

stringList = {"NppFTP", "FTPBox" , "tlp"}
uniqueLine = open('unique.txt', 'r', encoding='utf8', errors='ignore')
for line in uniqueLine:
if any(s in line for s in stringList):
print ("match found")

有谁知道如何从 stringList打印匹配的 string 而不是任何 string

提前致谢。

最佳答案

在不知道 unique.txt 看起来像什么的情况下,听起来您可以嵌套 for 和 if

stringList = {"NppFTP", "FTPBox" , "tlp"}
uniqueLine = open('unique.txt', 'r', encoding='utf8', errors='ignore')

for line in uniqueLine:
for s in stringList:
if s in line:
print ("match found for " + s)

关于python - 如何检查一行是否包含列表中的字符串并打印匹配的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55056059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com