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

前端 未结 19 1541
谎友^
谎友^ 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条回答
  •  -上瘾入骨i
    2020-11-22 02:12

    I had the same error, with URLs containing non-ascii chars (bytes with values > 128), my solution:

    url = url.decode('utf8').encode('utf-8')
    

    Note: utf-8, utf8 are simply aliases . Using only 'utf8' or 'utf-8' should work in the same way

    In my case, worked for me, in Python 2.7, I suppose this assignment changed 'something' in the str internal representation--i.e., it forces the right decoding of the backed byte sequence in url and finally puts the string into a utf-8 str with all the magic in the right place. Unicode in Python is black magic for me. Hope useful

提交回复
热议问题