簡體   English   中英

在文本python中搜索特定的單詞

[英]searching for specific words in a text python

我正在嘗試制作一個函數,該函數將一個參數(即一個單詞(或一組字符))以及語音作為參數,並返回一個布爾表達式來說明該單詞是否存在。

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()函數,但它似乎無法正常工作,我被卡住了。 如果有人幫助我解決這個問題,那將非常有幫助。 先感謝您

一種選擇是使用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')

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

為什么mmap不適用於大文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM