我找到了几个关于这个问题的网页,但没有一个解决了我的问题.
即使我做了:
pip show
Run Code Online (Sandbox Code Playgroud)
我明白了:
/usr/local/lib/python2.7/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.9.1) or chardet (2.3.0) doesn't match a supported version!
RequestsDependencyWarning)
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 480, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2691, in load_entry_point
return ep.load()
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2322, in load
return self.resolve()
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2328, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 74, in <module>
from pip.vcs import git, mercurial, subversion, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,它需要大量的文本作为输入,可以是任何字符编码,我想以UTF-8保存它们.我不会接收或不信任随数据定义的字符编码(如果有的话).
我有一段时间使用Pythons库chardet检测原始字符编码http://pypi.python.org/pypi/chardet,但最近遇到了一些问题,我发现它不支持斯堪的纳维亚语编码(例如ISO-8859-1).除此之外,获取结果需要大量的时间/ CPU/mem.对于2MB的文本文件,大约40秒.
我试过使用标准的Linux文件
file -bi name.txt
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的所有文件都为我提供了100%的结果.对于2MB的文件,这个约为0.1s.它也支持斯堪的纳维亚字符编码.
所以,我猜使用文件的优势很明显.有什么缺点?我错过了什么吗?
我想用lxml解析下载的RSS,但我不知道如何处理UnicodeDecodeError?
request = urllib2.Request('http://wiadomosci.onet.pl/kraj/rss.xml')
response = urllib2.urlopen(request)
response = response.read()
encd = chardet.detect(response)['encoding']
parser = etree.XMLParser(ns_clean=True,recover=True,encoding=encd)
tree = etree.parse(response, parser)
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
tree = etree.parse(response, parser)
File "lxml.etree.pyx", line 2692, in lxml.etree.parse (src/lxml/lxml.etree.c:49594)
File "parser.pxi", line 1500, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:71364)
File "parser.pxi", line 1529, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:71647)
File "parser.pxi", line 1429, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:70742)
File "parser.pxi", line 975, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:67
740)
File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etr
ee.c:63824)
File "parser.pxi", line 625, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:64745)
File "parser.pxi", line 559, …Run Code Online (Sandbox Code Playgroud) 问题是,对于一些上传到python应用程序的档案或文件,它ZipFile会namelist()返回错误解码的字符串.
from zip import ZipFile
for name in ZipFile('zipfile.zip').namelist():
print('Listing zip files: %s' % name)
Run Code Online (Sandbox Code Playgroud)
如何修复该代码,以便我总是解码unicode中的文件名(所以支持Chineeze,俄语和其他语言)?
我已经看到了Python 2的一些示例,但由于字符串的性质在python3中已经改变,我不知道如何重新编码它,或者在其上应用chardet.
我一直在尝试使用pip卸载chardet,但是我收到以下错误:
"无法卸载'chardet'.这是一个安装了distutils的项目,因此我们无法准确确定哪些文件属于它,这只会导致部分卸载."
我的pip版本是10.0.0,python 2.7.14,Ubuntu 14.04.
我正在尝试将数据集加载到 Pandas 中,但似乎无法通过第 1 步。我是新手,所以如果这很明显,请原谅,我已经搜索了以前的主题,但没有找到答案。数据大多是汉字,这可能是问题所在。
.csv 非常大,可以在这里找到:http : //weiboscope.jmsc.hku.hk/datazip/ 我在第 1 周尝试。
在下面的代码中,我确定了我尝试的 3 种解码类型,包括尝试查看使用的编码
import pandas
import chardet
import os
#this is what I tried to start
data = pandas.read_csv('week1.csv', encoding="utf-8")
#spits out error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9a in position 69: invalid start byte
#Code to check encoding -- this spits out ascii
bytes = min(32, os.path.getsize('week1.csv'))
raw = open('week1.csv', 'rb').read(bytes)
chardet.detect(raw)
#so i tried this! it also fails, which isn't that surprising since i …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Chardet制表符分隔格式推断一个非常大的文件(> 400 万行)的编码。
目前,我的脚本可能由于文件的大小而挣扎。我想将其缩小到加载文件的前 x 行,可能,但是当我尝试使用readline().
目前的脚本是:
import chardet
import os
filepath = os.path.join(r"O:\Song Pop\01 Originals\2017\FreshPlanet_SongPop_0517.txt")
rawdata = open(filepath, 'rb').readline()
print(rawdata)
result = chardet.detect(rawdata)
print(result)
Run Code Online (Sandbox Code Playgroud)
它有效,但它只读取文件的第一行。我尝试使用简单循环readline()多次调用并没有奏效(可能是脚本以二进制格式打开文件的事实)。
一行的输出是 {'encoding': 'Windows-1252', 'confidence': 0.73, 'language': ''}
我想知道增加它读取的行数是否会提高编码信心。
任何帮助将不胜感激。
我想尝试一些使用chardet 模块的代码。这是我在网上找到的代码:
import urllib2
import chardet
def fetch(url):
try:
result = urllib2.urlopen(url)
rawdata = result.read()
encoding = chardet.detect(rawdata)
return rawdata.decode(encoding['encoding'])
except urllib2.URLError, e:
handleError(e)
Run Code Online (Sandbox Code Playgroud)
但要尝试这个代码,我必须获得 chardet 模块:但是,我有两个选择: https: //pypi.python.org/pypi/chardet#downloads
我选择了 Python Wheel 并将此文件放在我的 Python27 目录中。但还是不行。
所以我的问题是: - 下载哪种类型的 chardet 文件 + 将该文件放在哪里,以便 Python 不打印此错误: Traceback (最近一次调用最后): File "C:/Python27/s7/test5.py", line 2、导入chardet时出现ImportError: No module named chardet
注意:(我使用的是Python 2.7)
预先感谢您的任何帮助或建议!:D
编辑1:抱歉,我是一个非常初学者,但事实上,必须选择Python源!特别是使用 setup.py 安装,但它对我不起作用!我打开 Windows 命令并写入 chardet-2.2.1(解压缩) 的路径,然后我写入: python setup.py install,但它不起作用...:S
我认为最好开设一个新科目。
我正在编写一个处理 CSV 文件的程序。这些文件可以具有特定的编码。我正在尝试合并一个过程来尝试猜测用户想要使用 chardet 打开的文件的编码。
\n\n我正在尝试使用以下代码:
\n\nrawdata = open(\'file.csv\', "r").read()\nresult = chardet.detect(rawdata)\nRun Code Online (Sandbox Code Playgroud)\n\n但我收到以下例外:
\n\n/usr/lib/python3.5/site-packages/chardet/__init__.py in detect(aBuf)\n 23 if ((version_info < (3, 0) and isinstance(aBuf, unicode)) or\n 24 (version_info >= (3, 0) and not isinstance(aBuf, bytes))):\n---> 25 raise ValueError(\'Expected a bytes object, not a unicode object\')\n 26 \n 27 from . import universaldetector\n\nValueError: Expected a bytes object, not a unicode object\nRun Code Online (Sandbox Code Playgroud)\n\n我也尝试过:
\n\nresult = chardet.detect(bytes(rawdata))\nRun Code Online (Sandbox Code Playgroud)\n\n但得到:
\n\nTypeError Traceback (most recent call last)\n<ipython-input-47-1137b0adb486> in …Run Code Online (Sandbox Code Playgroud) import chardet
a='haha'
print(chardet.detect(a))
Run Code Online (Sandbox Code Playgroud)
类型错误:字节或字节数组类型的预期对象,得到:< class 'str'>
我只是输入教程中的代码。我真的无法弄清楚发生了什么错误。
chardet ×10
python ×8
encoding ×4
python-3.x ×4
pip ×2
lxml ×1
pandas ×1
python-2.7 ×1
rss ×1
scraperwiki ×1
string ×1
unicode ×1
urllib3 ×1