要让python文件支持UTF-8输入和显示,我们熟悉在文件第二行加入如下注释: # -*- coding: <encoding name> -*- 但是为什么要这么写,到底写成什么格式是合法的,可以看下面这段说明: Defining the Encoding Python will default to ASCII as standard encoding if no other encoding hints are given. To define a source code encodi
用chardet检测编码 import chardet raw = open("model.json", 'rb').read() result = chardet.detect(raw) # 检测编码 encoding = result['encoding'] f = open("model.json", "r", encoding=encoding) lines = f.readlines() for line in lines: print