Skip to content

Instantly share code, notes, and snippets.

@buptxge
Created March 21, 2016 08:05
Show Gist options
  • Save buptxge/15874e27e612a7a2a8a2 to your computer and use it in GitHub Desktop.
Save buptxge/15874e27e612a7a2a8a2 to your computer and use it in GitHub Desktop.
import codecs
import chardet
bytes = min(32, os.path.getsize(filename))
raw = open(filename, 'rb').read(bytes)
if raw.startswith(codecs.BOM_UTF8):
encoding = 'utf-8-sig'
else:
result = chardet.detect(raw)
encoding = result['encoding']
infile = codecs.open(filename, mode, encoding=encoding)
data = infile.read()
infile.close()
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment