在Python中,如何从chardet模块开始?
我想尝试一些使用chardet模块的代码。 这是我在网上找到的代码:在Python中,如何从chardet模块开始?,python,python-2.7,encoding,character-encoding,chardet,Python,Python 2.7,Encoding,Character Encoding,Chardet,我想尝试一些使用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.UR
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)
但要尝试此代码,我必须获得chardet模块:
但是,我有两个选择:
- chardet-2.2.1-py2.py3-none-any.whl(md5)Python控制盘
- chardet-2.2.1.tar.gz(md5)Python源代码
我认为最好打开一个新主题。为什么不尝试使用pip安装它 Pip非常适合安装模块。只需下载get-pip.py并按照安装页面上的说明进行操作。安装后,请确保windows路径中包含以下内容: C:\Python27;C:\Python27\Tools\Scripts;C:\Python27\脚本
然后运行“pip install chardet”,它将安装最新版本的chardet,该版本将与您的python版本一起使用。您可以使用pip安装大多数模块,而无需下载源代码。在我看来,这比试图获取源文件要容易得多。注意,所描述的路径取决于python的版本和安装位置。例如,这也可能出现在类似
~\AppData\Local\Programs\Python\Python39\.