python自动识别文本编码格式代码

yipeiwu_com6年前Python基础

我就废话不多说了,直接上代码吧!

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import codecs
import os
import chardet
 
 
def detectCode(path):
	with open(path, 'rb') as file:
		data = file.read(200000)
		dicts = chardet.detect(data)
	return dicts["encoding"]
		
# 文件所在目录
if __name__ == '__main__':
	path = input("输入log文件路径: ")
	print(detectCode(path))

以上这篇python自动识别文本编码格式代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 捕获 shell/bash 脚本的输出结果实例

#!/usr/bin/python ## get subprocess module import subprocess   ## call date command ##...

对python 数据处理中的LabelEncoder 和 OneHotEncoder详解

如下所示: #简单来说 LabelEncoder 是对不连续的数字或者文本进行编号 from sklearn.preprocessing import LabelEncoder le...

使用PyQtGraph绘制精美的股票行情K线图的示例代码

使用PyQtGraph绘制精美的股票行情K线图的示例代码

pyqtgraph是Python平台上一种功能强大的2D/3D绘图库,相对于matplotlib库,由于其在内部实现方式上,使用了高速计算的numpy信号处理库以及Qt的Graphics...

跟老齐学Python之list和str比较

相同点 都属于序列类型的数据 所谓序列类型的数据,就是说它的每一个元素都可以通过指定一个编号,行话叫做“偏移量”的方式得到,而要想一次得到多个元素,可以使用切片。偏移量从0开始,总元素数...

python设置windows桌面壁纸的实现代码

复制代码 代码如下:# -*- coding: UTF-8 -*- from __future__ import unicode_literalsimport Imageimport...