[英]How to replace all those Special Characters with white spaces in python?
[英]python replace all special characters and spaces with single '-'
我試圖用單個' - '替換字符串中的所有特殊字符和空格
例如:
輸入: "Games & Fun"
輸出: "Games-Fun"
我試過了
>>> re.sub('[&" "]', '-', "Games & Fun")
'Games---Fun'
但我只想要"Games-Fun"
。
誰能幫我這個?
>>> import re
>>> text = "Games & Fun"
>>> re.sub(r'\W+', '-', text)
'Games-Fun'
>>> re.sub(r'[&\s]+', '-', "Games & Fun")
'Games-Fun'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.