import chardet from chardet import UniversalDetector def get_encoding(file): with open(file, "rb") as f: cs = chardet.detect(f.read()) return cs['encoding'] detector = UniversalDetector() with open(file, "rb") as f: for line in f.readlines(): detector.feed(line) if detector.done: break detector.close() return detector.result