Skip to content

Instantly share code, notes, and snippets.

@denisrasulev
Last active October 6, 2019 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denisrasulev/49fecb91be6f42ff328b25d47c3198b7 to your computer and use it in GitHub Desktop.
Save denisrasulev/49fecb91be6f42ff328b25d47c3198b7 to your computer and use it in GitHub Desktop.
Automatic file encoding detection
import chardet
import pandas as pd
def find_encoding(fname):
r_file = open(fname, 'rb').read()
result = chardet.detect(r_file)
charenc = result['encoding']
return charenc
my_encoding = find_encoding('myfile.csv')
df = pd.read_csv('myfile.csv', encoding=my_encoding)
# or
with open('myfile.csv', encoding=my_encoding) as f:
# do something
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment