python - 保持 url 中的文本干净

标签 python

作为 Python 中的信息检索项目(构建一个迷你搜索引擎)的一部分,我想从下载的推文中保留干净的文本(推文的 .csv 数据集 - 准确地说是 27000 条推文),一条推文将如下所示:

"The basic longing to live with dignity...these yearnings are universal. They burn in every human heart 1234." —@POTUS https://twitter.com/OZRd5o4wRL

"Democracy...allows us to peacefully work through our differences, and move closer to our ideals" —@POTUS in Greece https://twitter.com/PIO9dG2qjX

我想使用正则表达式删除推文中不需要的部分,例如 URL、标点符号等

所以结果会是:

"The basic longing to live with dignity these yearnings are universal They burn in every human heart POTUS"

"Democracy allows us to peacefully work through our differences and move closer to our ideals POTUS in Greece"

试过这个:pattern = RegexpTokenizer(r'[A-Za-z]+|^[0-9]'),但它做的并不完美,作为例如,URL 仍然存在于结果中。

请帮我找到一个能满足我要求的正则表达式模式。

最佳答案

这可能会有所帮助。

演示:

import re

s1 = """"Democracy...allows us to peacefully work through our differences, and move closer to our ideals" —@POTUS in Greece https://twitter.com/PIO9dG2qjX"""
s2 = """"The basic longing to live with dignity...these yearnings are universal. They burn in every human heart 1234." —@POTUS https://twitter.com/OZRd5o4wRL"""    

def cleanString(text):
    res = []
    for i in text.strip().split():
        if not re.search(r"(https?)", i):   #Removes URL..Note: Works only if http or https in string.
            res.append(re.sub(r"[^A-Za-z\.]", "", i).replace(".", " "))   #Strip everything that is not alphabet(Upper or Lower)
    return " ".join(map(str.strip, res))

print(cleanString(s1))
print(cleanString(s2))

关于python - 保持 url 中的文本干净,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51985530/

相关文章:

python - 如何减少python中的多个if语句

python - 如何定义一个在 Python 中永远不会匹配的变量?

python - 使用 Python 在 Linux、Windows 和 Mac 上列出磁盘驱动器的跨平台方式?

python - 在 Pandas 中有效地使用替换

javascript - 使用python zlib解压用javascript zlib压缩的字符串

python - "Fatal Python error: (pygame parachute) Segmentation Fault"改变窗口时

Python 的正则表达式星号量词没有按预期工作

python - 将 isalpha() 和 isspace() 合并为 1 条语句

python - 在 Pandas 数据框中相互获取最近点

python - 使用常规编码器使对象 JSON 可序列化