我收到了一些编码的文本,但我不知道使用了什么字符集。是否有一种使用Python确定文本文件编码的方法?如何检测文本文件的编码/代码页是关于C#的。
编辑:chardet 似乎已经不再维护,但大多数答案仍然适用。可以查看https://pypi.org/project/charset-normalizer/ 获取替代方案。
始终正确地检测编码是不可能的。
(来自 chardet 的常见问题解答)
然而,有些编码针对特定语言进行了优化,而语言并非随机的。一些字符序列总是出现, 而其他序列则毫无意义。精通英语的人打开报纸看到“txzqJv 2!dasd0a QqdKjvz”就会立即认为它不是英语(尽管它完全由英文字母组成)。通过研究大量的“典型”文本,计算机算法可以模拟这种流畅性,并对文本的语言进行教育猜测。
有一个名为chardet的库使用该方法来尝试检测编码。chardet 是 Mozilla 自动检测代码的移植版。
你也可以使用UnicodeDammit。它将尝试以下方法:
另一种查找编码的选项是使用libmagic(这是file命令背后的代码)。有大量可用的Python绑定。
生存在文件源代码树中的Python绑定可通过python-magic(或python3-magic)debian软件包获得。它可以通过执行以下操作来确定文件的编码:
import magic
blob = open('unknown-file', 'rb').read()
m = magic.open(magic.MAGIC_MIME_ENCODING)
m.load()
encoding = m.buffer(blob) # "utf-8" "us-ascii" etc
在pypi上也有一个名字相同但不兼容的python-magic pip软件包,它也使用libmagic
。它可以通过以下方式获取编码:
import magic
blob = open('unknown-file', 'rb').read()
m = magic.Magic(mime_encoding=True)
encoding = m.from_buffer(blob)
libmagic
确实是一个可替代chardet
的选择。关于名为python-magic
的不同包的信息很有用!我相信这种歧义会困扰很多人。 - MestreLionfile
并不是特别擅长识别文本文件中的人类语言。但它非常擅长识别各种容器格式,尽管有时您需要知道它的含义(例如,“Microsoft Office 文档”可能指 Outlook 消息等)。 - tripleeeopen()
函数:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 169799: invalid start byte
。根据vim的:set fileencoding
命令,该文件的编码为latin1
。 - xtianerrors='ignore'
,那么示例代码的输出将是不太有用的binary
。 - xtian一些编码策略,请按照自己的口味取消注释:
#!/bin/bash
#
tmpfile=$1
echo '-- info about file file ........'
file -i $tmpfile
enca -g $tmpfile
echo 'recoding ........'
#iconv -f iso-8859-2 -t utf-8 back_test.xml > $tmpfile
#enca -x utf-8 $tmpfile
#enca -g $tmpfile
recode CP1250..UTF-8 $tmpfile
您可能希望通过循环打开和读取文件来检查编码...但您可能需要先检查文件大小:
# PYTHON
encodings = ['utf-8', 'windows-1250', 'windows-1252'] # add more
for e in encodings:
try:
fh = codecs.open('file.txt', 'r', encoding=e)
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
print('opening the file with encoding: %s ' % e)
break
io
,例如 io.open(filepath, 'r', encoding='utf-8')
,这更加方便,因为 codecs
在读写时不会自动转换 \n
。更多信息请参见此处。 - Searene下面是一个读取文件并直接采用 chardet
编码预测结果的例子,当文件比较大时,只读取 n_lines
行。
chardet
还会给出它的编码预测的概率(即confidence
),以及从 chardet.predict()
返回的预测结果,如果您喜欢,可以将其与预测结果一起使用。
import chardet
from pathlib import Path
def predict_encoding(file_path: Path, n_lines: int=20) -> str:
'''Predict a file's encoding using chardet'''
# Open the file as binary data
with Path(file_path).open('rb') as f:
# Join binary lines for specified number of lines
rawdata = b''.join([f.readline() for _ in range(n_lines)])
return chardet.detect(rawdata)['encoding']
def predict_encoding(file_path, n=20):
... 省略 ... 然后
rawdata = b''.join([f.read() for _ in range(n)])
在 Python 3.6 中尝试了这个函数,对于 "ascii"、"cp1252"、"utf-8" 和 "unicode" 编码都能完美运行。所以这绝对值得点赞。 - n158这可能会有帮助
from bs4 import UnicodeDammit
with open('automate_data/billboard.csv', 'rb') as file:
content = file.read()
suggestion = UnicodeDammit(content)
suggestion.original_encoding
#'iso-8859-1'
all_codecs = ['ascii', 'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437',
'cp500', 'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', 'cp857',
'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866', 'cp869',
'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', 'cp1026', 'cp1125',
'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256',
'cp1257', 'cp1258', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213', 'euc_kr',
'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022_jp', 'iso2022_jp_1', 'iso2022_jp_2',
'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext', 'iso2022_kr', 'latin_1',
'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6', 'iso8859_7',
'iso8859_8', 'iso8859_9', 'iso8859_10', 'iso8859_11', 'iso8859_13',
'iso8859_14', 'iso8859_15', 'iso8859_16', 'johab', 'koi8_r', 'koi8_t', 'koi8_u',
'kz1048', 'mac_cyrillic', 'mac_greek', 'mac_iceland', 'mac_latin2', 'mac_roman',
'mac_turkish', 'ptcp154', 'shift_jis', 'shift_jis_2004', 'shift_jisx0213',
'utf_32', 'utf_32_be', 'utf_32_le', 'utf_16', 'utf_16_be', 'utf_16_le', 'utf_7',
'utf_8', 'utf_8_sig']
def find_codec(text):
for i in all_codecs:
for j in all_codecs:
try:
print(i, "to", j, text.encode(i).decode(j))
except:
pass
find_codec("The example string which includes ö, ü, or ÄŸ, ö")
这个脚本至少会生成9409行输出。如果输出无法适应终端屏幕,请尝试将输出写入文本文件中。
# Function: OpenRead(file)
# A text file can be encoded using:
# (1) The default operating system code page, Or
# (2) utf8 with a BOM header
#
# If a text file is encoded with utf8, and does not have a BOM header,
# the user can manually add a BOM header to the text file
# using a text editor such as notepad++, and rerun the python script,
# otherwise the file is read as a codepage file with the
# invalid codepage characters removed
import sys
if int(sys.version[0]) != 3:
print('Aborted: Python 3.x required')
sys.exit(1)
def bomType(file):
"""
returns file encoding string for open() function
EXAMPLE:
bom = bomtype(file)
open(file, encoding=bom, errors='ignore')
"""
f = open(file, 'rb')
b = f.read(4)
f.close()
if (b[0:3] == b'\xef\xbb\xbf'):
return "utf8"
# Python automatically detects endianess if utf-16 bom is present
# write endianess generally determined by endianess of CPU
if ((b[0:2] == b'\xfe\xff') or (b[0:2] == b'\xff\xfe')):
return "utf16"
if ((b[0:5] == b'\xfe\xff\x00\x00')
or (b[0:5] == b'\x00\x00\xff\xfe')):
return "utf32"
# If BOM is not provided, then assume its the codepage
# used by your operating system
return "cp1252"
# For the United States its: cp1252
def OpenRead(file):
bom = bomType(file)
return open(file, 'r', encoding=bom, errors='ignore')
#######################
# Testing it
#######################
fout = open("myfile1.txt", "w", encoding="cp1252")
fout.write("* hi there (cp1252)")
fout.close()
fout = open("myfile2.txt", "w", encoding="utf8")
fout.write("\u2022 hi there (utf8)")
fout.close()
# this case is still treated like codepage cp1252
# (User responsible for making sure that all utf8 files
# have a BOM header)
fout = open("badboy.txt", "wb")
fout.write(b"hi there. barf(\x81\x8D\x90\x9D)")
fout.close()
# Read Example file with Bom Detection
fin = OpenRead("myfile1.txt")
L = fin.readline()
print(L)
fin.close()
# Read Example file with Bom Detection
fin = OpenRead("myfile2.txt")
L =fin.readline()
print(L) #requires QtConsole to view, Cmd.exe is cp1252
fin.close()
# Read CP1252 with a few undefined chars without barfing
fin = OpenRead("badboy.txt")
L =fin.readline()
print(L)
fin.close()
# Check that bad characters are still in badboy codepage file
fin = open("badboy.txt", "rb")
fin.read(20)
fin.close()
原则上,在一般情况下,确定文本文件的编码是不可能的。因此,没有标准的Python库可以为您完成这项工作。
如果您对文本文件有更具体的了解(例如它是XML),那么可能会有库函数可用。
根据您所使用的平台,我选择使用 Linux shell 的file
命令。由于我在运行脚本时只在我们的 Linux 机器上运行,因此这对我来说很有效。
显然,这并不是一个理想的解决方案或答案,但可以修改以适应您的需求。在我的情况下,我只需要确定文件是否为 UTF-8。
import subprocess
file_cmd = ['file', 'test.txt']
p = subprocess.Popen(file_cmd, stdout=subprocess.PIPE)
cmd_output = p.stdout.readlines()
# x will begin with the file type output as is observed using 'file' command
x = cmd_output[0].split(": ")[1]
return x.startswith('UTF-8')
如果您知道文件的某些内容,可以尝试使用多种编码进行解码,并查看哪个缺失。一般来说,没有办法,因为文本文件就是文本文件,它们很愚蠢;)
chardet
有一些非常好的命令行界面,我不确定你的用例是什么,对我来说,我只是想在运行时猜测文件字符集,而不是在脚本或其他东西中使用它。要使用CLI选项(在pip install chardet
之后),请键入以下命令:$ chardet filename
,然后您可以使用猜测的编码将文本文件编码为其他选项,使用像iconv
这样的工具。 - adonese