Created
March 24, 2023 18:41
-
-
Save Rlyown/f27a94e1675aaa08f9fdd2e31e292dc7 to your computer and use it in GitHub Desktop.
A simple script to detect file encoding.
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
| #!/opt/homebrew/bin/python3 | |
| # coding: utf-8 | |
| from chardet.universaldetector import UniversalDetector | |
| import sys | |
| def help(): | |
| print("Usage: python encoding_detector.py <file> ...") | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| help() | |
| exit(1) | |
| for file in sys.argv[1:]: | |
| with open(file, "rb") as f: | |
| detector = UniversalDetector() | |
| for line in f: | |
| detector.feed(line) | |
| if detector.done: | |
| break | |
| detector.close() | |
| print(f"{file} --> {detector.result['encoding']}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment