How to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”

前端 未结 19 1561
谎友^
谎友^ 2020-11-22 01:21
as3:~/ngokevin-site# nano content/blog/20140114_test-chinese.mkd
as3:~/ngokevin-site# wok
Traceback (most recent call last):
File \"/usr/local/bin/wok\", line 4, in
         


        
19条回答
  •  情话喂你
    2020-11-22 02:13

    "UnicodeDecodeError: 'ascii' codec can't decode byte"
    

    Cause of this error: input_string must be unicode but str was given

    "TypeError: Decoding Unicode is not supported"
    

    Cause of this error: trying to convert unicode input_string into unicode


    So first check that your input_string is str and convert to unicode if necessary:

    if isinstance(input_string, str):
       input_string = unicode(input_string, 'utf-8')
    

    Secondly, the above just changes the type but does not remove non ascii characters. If you want to remove non-ascii characters:

    if isinstance(input_string, str):
       input_string = input_string.decode('ascii', 'ignore').encode('ascii') #note: this removes the character and encodes back to string.
    
    elif isinstance(input_string, unicode):
       input_string = input_string.encode('ascii', 'ignore')
    

提交回复
热议问题