0
使用Python,我試圖替換特殊字符在下面的文本:替換特殊字符的空間和創建列表
"The# dog.is.yelling$at!Me to"
爲空格,這樣以後我會得到清單:
['The','dog','is','yelling','at','me']
我該如何做到這一點?
使用Python,我試圖替換特殊字符在下面的文本:替換特殊字符的空間和創建列表
"The# dog.is.yelling$at!Me to"
爲空格,這樣以後我會得到清單:
['The','dog','is','yelling','at','me']
我該如何做到這一點?
您可以使用regular expressions:
>>> import re
>>> re.split("[#$!.\s]+", "The# dog.is.yelling$at!Me to")
['The', 'dog', 'is', 'yelling', 'at', 'Me', 'to']
或者通過字符的任何非字母數字序列分割,如指出@ thg435:
>>> re.split("\W+", "The# dog.is.yelling$at!Me to")
簡單're.split(R'\ W + 「)' – georg