如何检查 CSV 文件的编码

我有一个 CSV 文件,我希望了解它的编码。MicrosoftExcel 中是否有一个菜单选项可以帮助我检测它

或者我需要使用像 C # 或 PHP 这样的编程语言来推断它。

259934 次浏览

You can use Notepad++ to evaluate a file's encoding without needing to write code. The evaluated encoding of the open file will display on the bottom bar, far right side. The encodings supported can be seen by going to Settings -> Preferences -> New Document/Default Directory and looking in the drop down.

In Linux systems, you can use file command. It will give the correct encoding

Sample:

file blah.csv

Output:

blah.csv: ISO-8859 text, with very long lines

Use chardet https://github.com/chardet/chardet (documentation is short and easy to read).

Install python, then pip install chardet, at last use the command line command.

I tested under GB2312 and it's pretty accurate. (Make sure you have at least a few characters, sample with only 1 character may fail easily).

file is not reliable as you can see.

enter image description here

If you use Python, just use a print() function to check the encoding of a csv file. For example:

with open('file_name.csv') as f:
print(f)

The output is something like this:

<_io.TextIOWrapper name='file_name.csv' mode='r' encoding='utf8'>

In Python, You can Try...

from encodings.aliases import aliases
alias_values = set(aliases.values())


for encoding in set(aliases.values()):
try:
df=pd.read_csv("test.csv", encoding=encoding)
print('successful', encoding)
except:
pass

You can also use python chardet library

# install the chardet library
!pip install chardet


# import the chardet library
import chardet


# use the detect method to find the encoding
# 'rb' means read in the file as binary
with open("test.csv", 'rb') as file:
print(chardet.detect(file.read()))

Or you can execute in python console or in Jupyter Notebook:

import csv
data = open("file.csv","r")
data

You will see information about the data object like this:

<_io.TextIOWrapper name='arch.csv' mode='r' encoding='cp1250'>

As you can see it contains encoding infotmation.

Just add the encoding argument that matches the file you`re trying to upload.

open('example.csv', encoding='UTF8')

CSV files have no headers indicating the encoding.

You can only guess by looking at:

  • the platform / application the file was created on
  • the bytes in the file

In 2021, emoticons are widely used, but many import tools fail to import them. The chardet library is often recommended in the answers above, but the lib does not handle emoticons well.

icecream = '🍦'


import csv


with open('test.csv', 'w') as f:
wf = csv.writer(f)
wf.writerow(['ice cream', icecream])




import chardet
with open('test.csv', 'rb') as f:
print(chardet.detect(f.read()))


{'encoding': 'Windows-1254', 'confidence': 0.3864823918622268, 'language': 'Turkish'}

This gives UnicodeDecodeError while trying to read the file with this encoding.

The default encoding on Mac is UTF-8. It's included explicitly here but that wasn't even necessary... but on Windows it might be.

with open('test.csv', 'r', encoding='utf-8') as f:
print(f.read())


ice cream,🍦

The file command also picked this up

file test.csv
test.csv: UTF-8 Unicode text, with CRLF line terminators

My advice in 2021, if the automatic detection goes wrong: try UTF-8 before resorting to chardet.

As it is mentioned by @3724913 (Jitender Kumar) to use file command (it also works in WSL on Windows), I was able to get encoding information of a csv file by executing file --exclude encoding blah.csv using info available on man file as file blah.csv won't show the encoding info on my system.