在文本python中搜索特定单词

时间:2017-04-03 19:04:08

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

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

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)

我想这样做,以便程序在文本中搜索的单词与输入完全匹配,而不是另一个单词的一部分(" America" in" American&# 34)。我想过使用len()函数,但它似乎没有用,我被卡住了。如果有人帮我解决这个问题会非常有帮助。提前谢谢

2 个答案:

答案 0 :(得分:1)

您也可以使用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.

答案 1 :(得分:0)

一个选项可能是使用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')
相关问题