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))

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章