简体   繁体   English

将特殊字符替换为空格并创建列表

[英]replace special chars to space and create list

using Python, I'm trying to replace the special chars in the following text: 使用Python,我试图替换以下文本中的特殊字符:

"The# dog.is.yelling$at!Me to"

to spaces so that afterwards I would get the list: 到空格,以便之后我会得到列表:

['The','dog','is','yelling','at','me']

How do I do that in one line? 我怎么一行做到这一点?

You can use regular expressions : 您可以使用正则表达式

>>> import re
>>> re.split("[#$!.\s]+", "The# dog.is.yelling$at!Me to" )
['The', 'dog', 'is', 'yelling', 'at', 'Me', 'to']

Or to split by any non alpha-numeric sequence of chars, as pointed @thg435: 或者按照@ thg435所指出的任何非字母数字的字符序列进行拆分:

>>> re.split("\W+", "The# dog.is.yelling$at!Me to" )

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

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