首页 > 解决方案 > 用 python 和 re 清理文本

问题描述

我需要清理一些文本,如下面的代码所示:

import re
def clean_text(text):
    text = text.lower()
    #foction de replacement
    text = re.sub(r"i'm","i am",text)
    text = re.sub(r"she's","she is",text)
    text = re.sub(r"can't","cannot",text)
    text = re.sub(r"[-()\"#/@;:<>{}-=~|.?,]","",text)
    return text

clean_questions= []
for question in questions: 
    clean_questions.append(clean_text(question))

这段代码必须给我清空questions列表,但我questions清空了。我重新打开了spyder,列表已满,但没有被清理,然后重新打开它,我把它弄空了..控制台错误说:

In [10] :clean_questions= [] 
   ...: for question in questions: 
   ...: clean_questions.append(clean_text(question))
Traceback (most recent call last):

  File "<ipython-input-6-d1c7ac95a43f>", line 3, in <module>
    clean_questions.append(clean_text(question))

  File "<ipython-input-5-8f5da8f003ac>", line 16, in clean_text
    text = re.sub(r"[-()\"#/@;:<>{}-=~|.?,]","",text)

  File "C:\Users\hp\Anaconda3\lib\re.py", line 192, in sub
    return _compile(pattern, flags).sub(repl, string, count)

  File "C:\Users\hp\Anaconda3\lib\re.py", line 286, in _compile
   p = sre_compile.compile(pattern, flags)

  File "C:\Users\hp\Anaconda3\lib\sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)

  File "C:\Users\hp\Anaconda3\lib\sre_parse.py", line 930, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)

  File "C:\Users\hp\Anaconda3\lib\sre_parse.py", line 426, in _parse_sub
    not nested and not items))

  File "C:\Users\hp\Anaconda3\lib\sre_parse.py", line 580, in _parse
    raise source.error(msg, len(this) + 1 + len(that))

error: bad character range }-=

我正在使用 Python 3.6,特别是 Anaconda 构建 Anaconda3-2018.12-Windows-x86_64。

标签: pythonregexpython-3.xcharacter-class

解决方案


您的字符类(如回溯中所示)无效;}出现=在序数值之后(}是 125,=是 61),-它们之间的 in 表示它试图匹配从}' 序数到=' 以及介于两者之间的任何字符。由于字符范围必须从低序数到高序数,125->61 是无意义的,因此是错误的。

在某种程度上你很幸运;如果 周围的字符-被颠倒了,例如=-},你会默默地删除从序数 61 到 125 的所有字符,这将包括所有标准的 ASCII 字母,包括小写和大写,以及一堆标点符号。

您可以通过删除-角色类中的第二个来解决此问题(您已经将它包含在不需要转义的类的开头),从

text = re.sub(r"[-()\"#/@;:<>{}-=~|.?,]", "", text)

text = re.sub(r"[-()\"#/@;:<>{}=~|.?,]", "", text)

但我建议在这里删除正则表达式;大量文字标点符号出错的风险很高,还有其他方法根本不涉及正则表达式,它们应该可以正常工作,并且如果你逃脱了所有重要的东西也不会让你担心(替代方法是过度转义,这使得正则表达式不可读,并且仍然容易出错)。

相反,用一个简单的str.translatecall替换该行。首先,在函数之外,制作要删除的东西的翻译表

# The redundant - is harmless here since the result is a dict which dedupes anyway
killpunctuation = str.maketrans('', '', r"-()\"#/@;:<>{}-=~|.?,")

然后替换该行:

text = re.sub(r"[-()\"#/@;:<>{}-=~|.?,]","",text)

和:

text = text.translate(killpunctuation)

它应该至少与正则表达式一样快(可能更快),并且它更不容易出错,因为没有字符具有特殊含义(翻译表只是从 Unicode 序数到None,表示删除,另一个序数,表示单个字符替换, 或字符串,表示 char -> multichar 替换;它们没有特殊转义的概念)。如果目标是消除所有 ASCII 标点符号,则最好使用string模块常量来定义翻译表(这也使代码更具自我记录性,因此人们不会想知道您是删除所有标点符号还是仅删除一些标点符号,以及是否是故意的):

import string
killpunctuation = str.maketrans('', '', string.punctuation)

碰巧的是,您现有的字符串并没有删除所有标点符号(它遗漏了 , ^,!$),因此此更改可能不正确,但如果正确,请务必进行更改。如果它应该是标点符号的一个子集,您肯定要添加关于如何选择标点符号的注释,因此维护人员不会怀疑您是否犯了错误。


推荐阅读