繁体   English   中英

已清理的消息,仅包含字母 az 和数字 0-9,只有一个空格

[英]cleaned message, which contains only letters a-z, and numbers 0-9 with only one space

已清理的消息,仅包含字母 az 和数字 0-9,单词之间只有一个空格

def clean_data(message):
    return " ".join("".join(re.findall("[a-zA-Z0-9_ ]", message)).lower().split())

sentence_1 = 'Doesn\'t get, how{to}% \\operate+66.7 :after[it]"" & lt;# & gt; won\'t `or(what)'
sentence_2 = 'O\]k,.lar7i$double{} check wif*& da! hair: [dresser;   ..already He SaID-77.88.5 wun cut v short question(std txt rate)T&C\'s'
cleaned:  doesnt get howto operate667 afterit lt gt wont orwhat
cleaned:  oklar7idouble check wif da hair dresser already he said77885 wun cut v short questionstd txt ratetcs

预期 Output:

cleaned:    doesn t get how to operate 66 7 after it lt gt won t or what
cleaned:    o k lar7i double check wif da hair dresser already he said 77 88 5 wun cut v short question std txt rate t c s

从您发布的内容中,我们可以推断出以下几点:

  • Output 应该只有字母数字字符
  • 不是字母数字字符的所有内容都被替换为空格
  • 我们不应该有多个相邻的空格
  • Output 仅小写

假设这是预期的行为,代码非常简单:

def clean_data(message):
    return re.sub(r"[^\w]+", " ", message).lower()
  1. 您将使用[^\w]+获取大量不需要的字符,并在re.sub()的帮助下将它们替换为单个空格。

  2. 我们使用.lower()将所有内容转换为小写


sentence_1 = 'Doesn\'t get, how{to}% \\operate+66.7 :after[it]"" & lt;# & gt; won\'t `or(what)'
sentence_2 = 'O\]k,.lar7i$double{} check wif*& da! hair: [dresser;   ..already He SaID-77.88.5 wun cut v short question(std txt rate)T&C\'s'

print(clean_data(sentence_1))
print(clean_data(sentence_2))

>>> doesn t get how to operate 66 7 after it lt gt won t or what 
>>> o k lar7i double check wif da hair dresser already he said 77 88 5 wun cut v short question std txt rate t c s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM