使用Python进行文本挖掘

时间:2017-07-07 02:08:45

标签: python mining

我总共有900个“.txt”和“.htm”文件。每个文件有4个段落。每份文件都有理由说明公司为何从交易中退市。我只需要从所有文件中得到理由。公司暂停的原因通常是在“因为”和“”之类的单词之后。我如何使用python从所有文档中挖掘原因?我是python的新手。任何帮助都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

如果您知道暂停遵循特定单词,则可以使用正则表达式完成此操作。我在几分钟内为你做了一些示例代码。对于初学者,请从以下代码开始学习您不知道的内容。

from os import listdir
import re

for filename in listdir(directory): # directory = filepath to directory
    with open(filename, "r") as file:   # where your documents are located at
        contents = file.read()
    possibleSuspension = re.findall(r'(because of)[\w, ]*', contents)

答案 1 :(得分:0)

如果文档纯文本文件没有HTML标签,那么基本的正则表达式将帮助您完成工作。

如果你想解析HTML内容,可能更多地围绕提取的原因,请查看BeautifulSoup:https://www.crummy.com/software/BeautifulSoup/bs4/doc/

正则表达式(?<=This is)(.*)(?=sentence)

的示例

在这里试试你的regexp for python:https://regex101.com/