Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试仅用单个“-”替换字符串中的所有特殊字符和空格
例如:
输入:"Games & Fun"
"Games & Fun"
输出:"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'