【问题标题】:How to check if a value matches a txt file如何检查值是否与 txt 文件匹配
【发布时间】: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 &lt;genexpr&gt; 上说我相信问题是 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中?

标签: python for-loop any


【解决方案1】:

假设您只想打印 true 如果 keyword 在文件中,False 如果 keyword 不在文件中.. 尝试执行以下代码...

文本文件:: 999486 1117978 990583 1128062 1120618

程序::

def match_string(text):
    result = False
    keyword = [line.rstrip('\n') for line in open('keyword.txt')]
    if text in keyword:
        result = True
    return result

match_string('999487')

returns True

注意:我还是不明白你是需要匹配整个字符串还是匹配字符串的每个字符...

【讨论】:

  • 我只想匹配整个字符串,所以它基本上匹配 txt 文件:) 所以等“123456”需要匹配“123456”而不是字符! - 顺便说一句,在这种情况下我不需要遍历关键字吗?
  • 所以这里的问题是它退出了程序,因为我相信它没有循环通过关键字?
  • 如果要查看文件中是否存在'123456',并且文件中有一行是'123456789',是匹配还是不匹配?如果这应该是匹配的,那么您只需添加 for k in keyword 以包装 if text in keyword 并将其更改为 if text in k
  • 那么答案就在上面的代码中。它只是将文本文件中的所有关键字剥离到一个列表中,然后与所需的关键字匹配,如果找到则返回True,否则返回False。 ..
  • 同意。接受这个作为答案。既然您提到需要完全匹配,您甚至可能不必执行if text in keyword,而是执行if text == keyword。我想python会先做一个 str.size() 检查,所以如果长度不同,它会在 O(1) 时间内更快地捕获大多数不匹配情况。如果textkeyword 短,检查if text in keyword 将是O(n)
【解决方案2】:

这里不需要 re 模块,因为看起来我们只是在搜索字符串匹配。

import sys

KEYWORDS_PATH = 'keyword.txt'
KEYWORDS = open(KEYWORDS_PATH).read().splitlines()

sentences = ['999487']

for sentence in sentences:
    if sentence in KEYWORDS:
        print('Removed the keyword - %s' % sentence)
        sys.exit()

【讨论】:

  • 是的,这似乎是老实说的答案!我试过了,最终得到了我想要的!
【解决方案3】:

你可以试试这个:

text = "Some dummy text with numbers 123"
tokens = text.split(" ")
num = "123" # Number as string
if num in token:
    print("True")
else :
    print("False")

【讨论】:

    猜你喜欢
    • 2010-12-08
    • 2011-11-12
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多