-
-
Save karthicraghupathi/2db58a41d2705d5d641a0f6ca5bd8cbd to your computer and use it in GitHub Desktop.
Chardet (https://github.com/chardet/chardet) automatic open (from CLI example)
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
| from chardet.universaldetector import UniversalDetector | |
| def open_chardet(file, *args, **kwargs): | |
| detector = UniversalDetector() | |
| with open(file, mode='rb') as fh: | |
| for line in fh: | |
| line = bytearray(line) | |
| detector.feed(line) | |
| if detector.done: | |
| break | |
| detector.close() | |
| if detector.result['encoding']: | |
| print('Detected encoding: {} (confidence {})'.format( | |
| detector.result['encoding'], | |
| detector.result['confidence'] | |
| )) | |
| kwargs['encoding'] = detector.result['encoding'] | |
| return open(file, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment