Sunday, August 28, 2016

Python finding the encoding of a file

chardet is a universal character encoding detector in python. It can find the encoding of a file also provides a confidence score of the encoding. To have chardet in your environment, install chardet with your package manager. With pip it would look like

pip install chardet

Once chardet is installed, finding encoding of a file is very simple (if data.txt is a file)

chardetect data.txt

It comes back with "data.txt: ascii with confidence 1.0"

You can provide multiple files also to th chardet

chardetect data.txt chinese.txt 

It comes back with
data.txt: ascii with confidence 1.0
chinese.txt: utf-8 with confidence 0.99

Integrating chardet in a pthon program to find the encoding is as follows:

import chardet

file_name = 'data.txt'
result = chardet.detect(file_name)
print("Encoding: " + result['encoding'] + " Confidence " +  str(result['confidence']))

1 comment:

  1. Ok, thank you for sharing this information it is useful for me. Also I wanted to say, when I had problems with open .txt file I found the solution on this webstie. So, now no problems any more.