Last active
October 6, 2019 08:22
-
-
Save denisrasulev/49fecb91be6f42ff328b25d47c3198b7 to your computer and use it in GitHub Desktop.
Automatic file encoding detection
This file contains 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
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