简体   繁体   中英

python: replace non-“normal” characters in a string

While iterating through different strings I sometimes stumble upon characters that contain strings such as "ś" or even just a " ' " and I want to replace those every single character with a simple "_".

So the code I would want is one that meets the following requirements: if character is not a,b,c....Z or a number then character = "_".

I wonder what is the most elegant way to implement such a code.

Thanks in advance.

import re

s = re.sub('[^A-Za-z0-9]', '_', s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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