【发布时间】:2019-01-08 12:34:57
【问题描述】:
我目前正在尝试解决一个解决方案,其中我有一个值和一个文本文件 (.txt),我想在其中检查代码中的值是否在文本文件中的某个位置。
我目前所做的是我有一个看起来像这样的文本文件:
999486
1117978
990583
1128062
1120618
以及如下代码:
def filter():
item_name = '1128062'
keyword = [line.rstrip('\n') for line in open('keywords.txt')]
has_good = False
sentences = [item_name]
def check_all(sentence, ws):
return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)
for sentence in sentences:
if any(check_all(sentence, word) for word in keyword):
has_good = True
break
if not has_good or keyword == "":
print("Removed the keyword - " + str(item_name))
sys.exit()
脚本的作用是:
它有一个 item_name 有一个值。 打开存储所有关键字的关键字
使用 check_all 函数和 for sentence in sentence: 我的想法是检查 txt 文件中的关键字是否匹配。如果是,那么我们就继续程序,如果不是,则打印出 Removed the keyword and sys.exit the program.
但是,当我现在尝试运行此程序时,我收到一个错误提示
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\PC\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/test.py.py", line 324, in filter
if any(check_all(sentence, word) for word in keyword):
File "C:/Users/test.py.py", line 324, in <genexpr>
if any(check_all(sentence, word) for word in keyword):
File "C:/Users/test.py.py", line 321, in check_all
return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)
File "C:/Users/test.py.py", line 321, in <genexpr>
return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)
File "C:\Users\PC\AppData\Local\Programs\Python\Python36\lib\re.py", line 182, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object
我意识到这一定是个问题
def check_all(sentence, ws):
return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)
这就是我遇到问题的地方,问你们我如何能够检查 .txt 文件中的关键字是否匹配,如果不匹配,则打印出 Removed the keyword 和 sys.exit 程序,如果匹配,我们什么都不做。
【问题讨论】:
-
@NoorJafri 你好!嗯,我不确定,但我认为这就是我收到错误的原因,因为它在错误输出
File "C:/Users/test.py.py", line 321, in <genexpr>上说我相信问题是for w in ws我相信它会逐个字符地打印? -
我在运行您的代码 sn-p 时无法重现该错误。您可以尝试将您的
w转换为str吗?如下:return all(re.search(r'\b{}\b'.format(str(w)), sentence) for w in ws) -
好吧,所以我刚刚尝试并意识到
for w in ws会逐个字符地循环,我不确定这是否是它失败的原因?因为我试图打印出ws,它给了我整个关键字,而for w in ws给了我一个字符一个字符。 @Endyd -
对,因为在 for 循环中,你有一个嵌套的 for 循环,所以你在外部 for 循环(
for sentence in sentences)中逐个关键字执行,然后在 @ 中逐个字符执行当你说for word in keyword时,987654334@声明,实际上是for char in keyword。 -
是的,我也相信。我认为网站的问题。所以意思是我需要以某种方式解决检查
sentence是否在keyword中?