Last active
December 18, 2019 06:14
-
-
Save mitsu-ksgr/a8d048083c0634021487eb43c6b17a02 to your computer and use it in GitHub Desktop.
Detect file encoding in python3 with chardet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# coding: utf-8 | |
import os | |
import sys | |
from chardet.universaldetector import UniversalDetector | |
def detect_encoding(file_path): | |
detector = UniversalDetector() | |
detector.reset() | |
with open(file_path, mode='rb') as f: | |
for b in f: | |
detector.feed(b) | |
if detector.done: break | |
detector.close() | |
return detector.result | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print('Usage: detect_encoding path_to_file') | |
exit() | |
import chardet | |
print('chardet version: {0}'.format(chardet.__version__)) | |
for fpath in sys.argv[1:]: | |
encode = detect_encoding(fpath) | |
print('{0}* {1}'.format(os.linesep, fpath)) | |
for k, v in encode.items(): | |
print('{0}: {1}'.format(k.title(), v)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment