Regular expression that finds and replaces non-ascii characters with Python

前端 未结 7 2226
無奈伤痛
無奈伤痛 2020-12-03 19:40

I need to change some characters that are not ASCII to \'_\'. For example,

Tannh‰user -> Tannh_user
  • If I use regular expression with Python, how
7条回答
  •  一个人的身影
    2020-12-03 20:10

    re.sub(r'[^\x00-\x7F]', '_', theString)
    

    This will work if theString is unicode, or a string in an encoding where ASCII occupies values 0 to 0x7F (latin-1, UTF-8, etc.).

提交回复
热议问题