python加中文引起的編碼錯誤解決方法

2021-08-31 18:18:14 字數 494 閱讀 7574

python檔案中有時加入中文後會出現如下類似錯誤: 

syntaxerror: non-ascii character '\xe4' in file d:\anaconda2\envs\labelme\lib\site-packages\labelme\utils2.py on line 1, but no encoding declared; see for detail
在python檔案起始加入下面兩行注釋即可解決

#!/usr/bin/env python

# encoding: utf-8

#!/usr/bin/env python   //告訴linux系統,這個是python可執行程式

# _*_ coding:utf-8 _*_  //告訴python直譯器,按照utf-8編碼讀取源**,否則,你在源**中的中文輸出可能會由亂碼

ensure ascii 引數引起的中文編碼問題

在使用json.dumps時要注意乙個問題 import json print json.dumps 中國 u4e2d u56fd 輸出的會是 中國 中的ascii 字元碼,而不是真正的中文。這是因為json.dumps 序列化時對中文預設使用的ascii編碼.想輸出真正的中文需要指定ensure ...

Python 中文編碼

python 檔案中如果未指定編碼,在執行過程會出現報錯 usr bin python print 你好,世界 以上程式執行輸出結果為 file test.py line 2 syntaxerror non ascii character xe4 in file test.py on line 2,...

Python 中文編碼

在python中如果輸出中文字元 你好,世界 就有可能會碰到中文編碼問題。python 檔案中如果未指定編碼,在執行過程會出現報錯 usr bin python print 你好,世界 以上程式執行輸出結果為 file test.py line 2syntaxerror non ascii char...