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

前端 未结 7 2209
無奈伤痛
無奈伤痛 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:07

    Updated for Python 3:

    >>> 'Tannh‰user'.encode().decode('ascii', 'replace').replace(u'\ufffd', '_')
    'Tannh___user'
    

    First we create byte string using encode() - it uses UTF-8 codec by default. If you have byte string then of course skip this encode step. Then we convert it to "normal" string using the ascii codec.

    This uses the property of UTF-8 that all non-ascii characters are encoded as sequence of bytes with value >= 0x80.


    Original answer – for Python 2:

    How to do it using built-in str.decode method:

    >>> 'Tannh‰user'.decode('ascii', 'replace').replace(u'\ufffd', '_')
    u'Tannh___user'
    

    (You get unicode string, so convert it to str if you need.)

    You can also convert unicode to str, so one non-ASCII character is replaced by ASCII one. But the problem is that unicode.encode with replace translates non-ASCII characters into '?', so you don't know if the question mark was there already before; see solution from Ignacio Vazquez-Abrams.


    Another way, using ord() and comparing value of each character if it fits in ASCII range (0-127) - this works for unicode strings and for str in utf-8, latin and some other encodings:

    >>> s = 'Tannh‰user' # or u'Tannh‰user' in Python 2
    >>> 
    >>> ''.join(c if ord(c) < 128 else '_' for c in s)
    'Tannh_user'
    
    0 讨论(0)
  • 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.).

    0 讨论(0)
  • 2020-12-03 20:13

    Using Python's support for character encodings:

    # coding: utf8
    import codecs
    
    def underscorereplace_errors(exc):
      return (u'_', exc.end)
    
    codecs.register_error('underscorereplace', underscorereplace_errors)
    
    print u'Tannh‰user'.encode('ascii', 'underscorereplace')
    
    0 讨论(0)
  • 2020-12-03 20:14

    if you know which characters you want to replace, you can apply string methods

    mystring.replace('oldchar', 'newchar')
    
    0 讨论(0)
  • 2020-12-03 20:19

    With the magical regex [ -~] one can solve it:

    import re
    re.sub(r"[^ -~]", "_", "Tannh‰user")
    # 'Tannh_user'
    

    Explanation:

    • The ascii characters are the symbols ranging from " " to "~" - hence [ -~] captures all ascii chars
    • By appending ^we can capture all non-ascii characters
    • The rest is now a formality
    0 讨论(0)
  • 2020-12-03 20:20

    I'd rather just call ord on every character in the string, 1 by 1. If ord([char]) >= 128 the character is not an ascii character and should be replaced.

    0 讨论(0)
提交回复
热议问题