如下代码就是通过chardet包获得文件的编码格式的,其中chardet需要单独安装
import chardet
byte=min(32,os.path.getsize(filename))
raw=open(filename,'rb').read(byte)
result=chardet.detect(raw)
encoding=result['encoding']
本文介绍了一种使用Python的chardet包检测文件编码格式的方法。通过读取文件的前32字节,chardet能够自动识别出文件的编码类型,这对于处理未知编码的文件非常有用。
如下代码就是通过chardet包获得文件的编码格式的,其中chardet需要单独安装
import chardet
byte=min(32,os.path.getsize(filename))
raw=open(filename,'rb').read(byte)
result=chardet.detect(raw)
encoding=result['encoding']

被折叠的 条评论
为什么被折叠?