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

前端 未结 19 1781
谎友^
谎友^ 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:00

    In short, to ensure proper unicode handling in Python 2:

    • use io.open for reading/writing files
    • use from __future__ import unicode_literals
    • configure other data inputs/outputs (e.g., databases, network) to use unicode
    • if you cannot configure outputs to utf-8, convert your output for them print(text.encode('ascii', 'replace').decode())

    For explanations, see @Alastair McCormack's detailed answer.

提交回复
热议问题