UnicodeEncodeError: 'ascii' codec can't encode character in position 0: ordinal not in range(128)

前端 未结 4 1876
一整个雨季
一整个雨季 2020-12-04 18:00

I\'m working on a Python script that uses the scissor character (9986 - ✂) and I\'m trying to port my code to Mac, but I\'m running into this error.

The scissor char

4条回答
  •  [愿得一人]
    2020-12-04 18:45

    test_io_encoding.py output suggests that you should change your locale settings e.g., set LANG=en_US.UTF-8.


    The first error might be due to you are trying to decode a string that is already Unicode. Python 2 tries to encode it using a default character encoding ('ascii') before decoding it using (possibly) different character encoding. The error happens on the encode step:

    >>> u"\u2702".decode() # Python 2
    Traceback (most recent call last):
      File "", line 1, in 
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u2702' in position 0: ordinal not in range(128)
    

    It looks like you are running your script using Python 2 instead of Python 3. You would get:

    >>> "\u2702".decode() # Python 3
    Traceback (most recent call last):
      File "", line 1, in 
    AttributeError: 'str' object has no attribute 'decode'
    

    different error otherwise.

    Just drop the .decode() call:

    print("|\t {0} PySnipt'd {0} \t|".format(snipper))
    

    The second issue is due to printing a Unicode string into a pipe:

    $ python3 -c'print("\u2702")'
    ✂
    $ python3 -c'print("\u2702")' | cat
    Traceback (most recent call last):
      File "", line 1, in 
    UnicodeEncodeError: 'ascii' codec can't encode character '\u2702' in position 0: ordinal not in range(128)
    

    Set appropriate for your purpose PYTHONIOENCODING environment variable:

    $ PYTHONIOENCODING=utf-8 python3 -c'print("\u2702")' | cat
    ✂
    

    the terminal is just displaying this: | b'\xe2\x9c\x82' PySnipt'd b'\xe2\x9c\x82' |

    If snipper is a bytes object then leave the snipper.decode() calls.

    $ python3 -c"print(b'\xe2\x9c\x82'.decode())"
    ✂
    $ python3 -c"print(b'\xe2\x9c\x82'.decode())" | cat
    Traceback (most recent call last):
      File "", line 1, in 
    UnicodeEncodeError: 'ascii' codec can't encode character '\u2702' in position 0: ordinal not in range(128)
    

    The fix is the same:

    $ PYTHONIOENCODING=utf-8 python3 -c"print(b'\xe2\x9c\x82'.decode())" | cat
    ✂
    

提交回复
热议问题