0

我想让用户选择并打开多个文本并在文本中搜索完全匹配的内容。我希望编码是 unicode。

如果我搜索“cat”,我希望它找到“cat”、“cat”、“.cat”,而不是“catalogue”。

我不知道如何让用户同时在所有文本中搜索两个词(“猫”或“狗”)??????也许我可以使用RE?

到目前为止,我刚刚使用户可以插入包含要搜索的文本文件的目录的路径。现在我想让用户(raw_input)在所有文本中搜索两个单词,然后打印和将结果(例如 document1.txt 中的“search_word_1”和“search_word_2”,document4.txt 中的“search_word_2”)保存在单独的文档 (search_words) 中。

import re, os


path = raw_input("insert path to directory :")
ex_library = os.listdir(path)
search_words = open("sword.txt", "w") # File or maybe list to put in the results
thelist = []

for texts in ex_library:
    f = os.path.join(path, texts)
    text = open(f, "r")
    textname = os.path.basename(texts)
    print textname
    for line in text.read():

    text.close()
4

3 回答 3

1

在这种情况下,正则表达式是合适的工具。

我希望它找到“cat”、“cat”、“.cat”而不是“catalogue”。

图案:r'\bcat\b'

\b在单词边界匹配。

如何让用户同时在所有文本中搜索两个词(“猫”或“狗”)

图案:r'\bcat\b|\bdog\b'

打印"filename: <words that are found in it>"

#!/usr/bin/env python
import os
import re
import sys

def fgrep(words, filenames, encoding='utf-8', case_insensitive=False):
    findwords = re.compile("|".join(r"\b%s\b" % re.escape(w) for w in words),
                           flags=re.I if case_insensitive else 0).findall
    for name in filenames:
        with open(name, 'rb') as file:
             text = file.read().decode(encoding)
             found_words = set(findwords(text))
             yield name, found_words

def main():
    words = [w.decode(sys.stdin.encoding) for w in sys.argv[1].split(",")]
    filenames = sys.argv[2:] # the rest is filenames
    for filename, found_words in fgrep(words, filenames):
        print "%s: %s" % (os.path.basename(filename), ",".join(found_words))

main()

例子:

$ python findwords.py 'cat,dog' /path/to/*.txt

替代解决方案

为了避免读取内存中的整个文件:

import codecs

...
with codecs.open(name, encoding=encoding) as file:
    found_words = set(w for line in file for w in findwords(line))

您还可以在找到的上下文中打印找到的单词,例如,打印带有突出显示的单词的行:

from colorama import init  # pip install colorama
init(strip=not sys.stdout.isatty())  # strip colors if stdout is redirected
from termcolor import colored  # pip install termcolor

highlight = lambda s: colored(s, on_color='on_red', attrs=['bold', 'reverse'])

...
regex = re.compile("|".join(r"\b%s\b" % re.escape(w) for w in words),
                   flags=re.I if case_insensitive else 0)

for line in file:
    if regex.search(line): # line contains words
       line = regex.sub(lambda m: highlight(m.group()), line)
       yield line
于 2012-12-27T12:03:55.550 回答
0

您需要将每个文件中的文本拆分为空格和标点符号。完成后,您只需在剩余列表中查找要搜索的单词。您还需要将所有内容都转换为小写,除非您还需要区分大小写的搜索。

于 2012-12-27T11:32:27.637 回答
0

除了现有答案之外,还有一些(可能有用的)信息:

您应该知道,当用户想到“字符”(=grapheme)时,他的意思并不总是与 Unicode 字符相同,并且某些字素可以由 Unicode 字符以不止一种独特的方式表示(例如复合字符与基本字符+组合标记)。

要基于字形(=用户在大多数情况下的期望)而不是特定的 Unicode 字符序列进行搜索,您需要在搜索之前对字符串进行规范化。

于 2012-12-27T12:51:58.280 回答