Lookuperror : unknown encoding : utf-8

thebjorn BjornSteinarFjeldPettersen at gmail.com
Mon Oct 30 02:47:41 EST 2006


Sachin Punjabi wrote:
> I wanted to read a file encoded in utf-8 and and using the following
> syntax in my source which throws me an error specifying Lookuperror :
> unknown encoding : utf-8. Also I am working on Python version 2.4.1.

You shouldn't have to do anything to have the utf-8 encoding available.
Check in your lib/encodings directory for a file name utf_8.py and the
code in __init__.py in the same directory should take care of the
mapping. This has been this way since at least Python 2.2 (which is the
oldest version I have on this machine).

If that doesn't give you a clue as to what is going on in your setup,
try

  u'foo'.encode('utf-8')

at the prompt and post the complete traceback.

> import codecs
> fileObj = codecs.open( "data.txt", "r", "utf-8" )

That should work fine, although I prefer to explicitly set the mode to
"rb" (it will be set to binary mode behind your back regardless ;-)

hth,
-- bjorn




More information about the Python-list mailing list