Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the encoding of a file in Python? [duplicate]

Does anybody know how to get the encoding of a file in Python. I know that you can use the codecs module to open a file with a specific encoding but you have to know it in advance.

import codecs f = codecs.open("file.txt", "r", "utf-8") 

Is there a way to detect automatically which encoding is used for a file?

Thanks in advance

Edit: Thanks everybody for very interesting answsers. You may also be interested by http://whatismyencoding.com/ which is based on chardet (more over the site is powered by bottle python framework)

like image 917
luc Avatar asked Jan 27 '10 05:01

luc


People also ask

How do you check what encoding a file is in?

In Visual Studio, you can select "File > Advanced Save Options..." The "Encoding:" combo box will tell you specifically which encoding is currently being used for the file.

How do I check if a file is UTF-8 encoded in Python?

Could be simpler by using only one line: codecs. open("path/to/file", encoding="utf-8", errors="strict").

How do I know if my file is UTF-8 or ANSI?

Open the file using Notepad++ and check the "Encoding" menu, you can check the current Encoding and/or Convert to a set of encodings available.


1 Answers

Unfortunately there is no 'correct' way to determine the encoding of a file by looking at the file itself. This is a universal problem, not limited to python or any particular file system.

If you're reading an XML file, the first line in the file might give you a hint of what the encoding is.

Otherwise, you will have to use some heuristics-based approach like chardet (one of the solutions given in other answers) which tries to guess the encoding by examining the data in the file in raw byte format. If you're on Windows, I believe the Windows API also exposes methods to try and guess the encoding based on the data in the file.

like image 147
HS. Avatar answered Oct 16 '22 19:10

HS.