当前位置: 移动技术网 > IT编程>网页制作>CSS > python查看文件的编码方式

python查看文件的编码方式

2018年12月05日  | 移动技术网IT编程  | 我要评论

python查看文件的编码方式

最近在实验楼上做练习,遇到了中文乱码的问题。因为在notepad上能够正确显示文件内容,但是使用sublime3打开时,则为中文乱码。
这种乱码问题大都是因为编码和解码的不匹配造成的。通过notepad可以将其他编码下转为utf-8的编码方式。
另外有时想确定某一文件的编码方式,可以通过以下方法确认。

import chardet

def get_encoding(file):
	with open(file,'rb') as f:
		return chardet.detect(f.read())['encoding']

file_name="c:\\users\\administrator\\desktop\\stations.py"  #此处替换为你自己的文件路径
encoding = get_encoding(file_name)
print(encoding)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网