In Python, how to begin with chardet module?

23.9k views Asked by At

I would like to try some code that uses the chardet module. This is the code i have found on the web :

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)

But to try this code, i have to get the chardet module : But, i have two choices : https://pypi.python.org/pypi/chardet#downloads

  • chardet-2.2.1-py2.py3-none-any.whl (md5) Python Wheel
  • chardet-2.2.1.tar.gz (md5) Python source

I have chosen the Python Wheel and put this file in my Python27 directory. But still does not work.

So my problems are : - which type of chardet file to download + where to put this file for Python not to print this error : Traceback (most recent call last): File "C:/Python27/s7/test5.py", line 2, in import chardet ImportError: No module named chardet

Note :(I'm on Python 2.7)

Thanks in advance for any help or suggestions ! :D

EDIT 1 : Sorry for being a very beginner, but in fact it is the python source that must be chosen! Especially, installing with setup.py, but it does not work to me ! I opened the Windows command and wrote the path to the chardet-2.2.1(unzipped) , and then i wrote : python setup.py install, but it does not work ...:S

I think it's better to open a new subject.

1

There are 1 answers

2
H0L0GH05t On

Why not try installing it using pip?

https://pip.pypa.io/en/latest/installation/

Pip is great for installing modules. Just download the get-pip.py and follow the instructions on the installation page. Once it's installed, make sure these are in your windows path: C:\Python27;C:\Python27\Tools\Scripts;C:\Python27\Scripts;

Then just run "pip install chardet" and it will install the latest version of chardet that will work with your version of python. You can use pip to install most modules without having to download the source. It's a lot easier than trying to get source files in my opinion.