【问题标题】:searching for specific words in a text python在文本python中搜索特定单词
【发布时间】:2017-08-28 18:39:52
【问题描述】:

我正在尝试创建一个函数,该函数将接受一个词(或一组字符)以及语音的参数,并返回一个布尔表达式,说明该词是否存在,作为一个函数。

speech2 = open("Obama_DNC.txt", "r")
speech2_words = speech2.read()
def search(word):
    if word in speech2_words:
        if len(word) == len(word in speech2_words):
            print(True)
        elif len(word) != len(word in speech2_words):
            print(False)
    elif not word in speech2_words:
        print(False)


word = input("search?")
search(word)

我想让程序在文本中搜索的单词与输入完全匹配,并且不属于另一个单词(“American”中的“America”)。我想过使用 len() 函数,但它似乎不起作用,我被卡住了。如果有人帮助我解决这个问题,那将非常有帮助。提前谢谢你

【问题讨论】:

  • len(word in speech2_words) 无效:您将布尔值传递给 len。您必须拆分字符串或使用正则表达式并使用 r"\b"+word+r"\b" 进行搜索

标签: python function for-loop if-statement text-files


【解决方案1】:

一种选择可能是使用regex 模块中的findall() 方法,该方法可用于查找特定字符串的所有匹配项。

或者,您可以包含list.count() 以检查搜索的字符串在文本中出现的次数:

import re

def search(word):
    found = re.findall('\\b' + word + '\\b', speech2_words)
    if found:
        print(True, '{word} occurs {counts} time'.format(word=word, counts=found.count(word)))
    else:
        print(False)

输出:

search?America
(True, 'America occurs 28 time')
search?American
(True, 'American occurs 12 time')

【讨论】:

    【解决方案2】:

    您也可以使用 mmap,了解更多关于mmap的信息

    python 3 中的 mmap 与 python 2.7 中的处理方式不同

    以下代码适用于 2.7,它在文本文件中查找字符串的作用。

    #!/usr/bin/python
    
    import mmap
    f = open('Obama_DNC.txt')
    s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
    if s.find('blabla') != -1:
        print 'true'
    

    Why mmap doesnt work with large files.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-31
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多