我使用chardet来识别我的文件编码,但是发生了这个错误:
fh= open("file", mode="r")
sc= chardet.detect(fh)
Traceback (most recent call last):
File "/home/alireza/test.py", line 19, in <module>
sc= chardet.detect(fh)
File "/usr/lib/python3/dist-packages/chardet/__init__.py", line 24, in detect
u.feed(aBuf)
File "/usr/lib/python3/dist-packages/chardet/universaldetector.py", line 65, in feed
aLen = len(aBuf)
TypeError: object of type '_io.TextIOWrapper' has no len()如果不知道编码我就不能打开文件,
fh= open("file", mode="r").read()
sc= chardet.detect(fh)
Traceback (most recent call last):
File "/home/alireza/workspacee/makecdown/test.py", line 21, in <module>
fh= open("910.srt", mode="r").read()
File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 34: invalid continuation byte没有打开文件怎么使用chardet ?!或者有什么方法可以在打开之前/之后找出文件编码?
发布于 2012-11-21 20:42:03
试着像这样打开文件
fh= open("file", mode="rb")命令行工具
如果这不起作用,请尝试chardet的命令行工具。来自https://github.com/erikrose/chardet的描述
chardet附带一个命令行脚本,该脚本报告一个或多个文件的编码:
% chardetect.py某个其他文件某个文件: windows-1252,置信度为0.5其他文件: ascii,置信度为1.0
发布于 2012-11-21 23:41:40
不是一个直接的答案,但你可以在http://getpython3.com/diveintopython3/case-study-porting-chardet-to-python-3.html找到Python3中的工作原理描述。学习之后,您可能会发现如何检测另一个特定的编码。
该代码最初是从Mozilla Seamonkey派生的。你也可以在那里找到更多信息。
https://stackoverflow.com/questions/13478592
复制相似问题