Skip to content

Instantly share code, notes, and snippets.

@Rlyown
Created March 24, 2023 18:41
Show Gist options
  • Save Rlyown/f27a94e1675aaa08f9fdd2e31e292dc7 to your computer and use it in GitHub Desktop.
Save Rlyown/f27a94e1675aaa08f9fdd2e31e292dc7 to your computer and use it in GitHub Desktop.
A simple script to detect file encoding.
#!/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