Lookuperror : unknown encoding : utf-8

S

Sachin Punjabi

Hi,

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.

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

Can anyone please guide me how do I get utf-8 activated in my codecs or
any setting needs to be done for the same before using codecs.

Regards
Sachin Punjabi.
 
L

Leo Kislov

Sachin said:
Hi,

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.

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

Can anyone please guide me how do I get utf-8 activated in my codecs or
any setting needs to be done for the same before using codecs.

What OS? Where did you get your python distribution? Anyway, I believe
utf-8 codec was in the python.org distribution since the introduction
of unicode (around python 2.0). If you can't use utf-8 codec right out
of the box, something is really wrong with your setup.

-- Leo
 
T

thebjorn

Sachin said:
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
 
S

Sachin Punjabi

utf-8 codec was in the python.org distribution since the introduction
of unicode (around python 2.0). If you can't use utf-8 codec right out
of the box, something is really wrong with your setup.

-- Leo

The OS is Windows XP and also how do I incorporate python distribution.
Disutils folder exists in the python folder. Anything I need to do
there ?

Sachin.
 
F

Fredrik Lundh

Sachin said:
The OS is Windows XP

then your installation is seriously broken. where did you get the
installation kit? have you removed stuff from the Lib directory ?

</F>
 
S

Sachin Punjabi

installation kit? have you removed stuff from the Lib directory ?

</F>

It was already installed on my PC and I have no clue how it was
installed or any changes has been done. I am just downloading newer
version from python.org and will install and check it. I think there
should be problem with installation itself.

Thanx
Sachin.
 
L

Leo Kislov

Sachin said:
It was already installed on my PC and I have no clue how it was
installed or any changes has been done.

Then it's a distribution of your PC manufacturer. They could omit some
modules like utf-8 codec.
I am just downloading newer
version from python.org and will install and check it. I think there
should be problem with installation itself.

That's a right idea, I'd also recommend to leave the manufacturer's
python distribution alone. Do not remove it, do not upgrade it. Some
programs provided by the manufacturer can stop working. If the
preinstalled python was installed into c:\python24 directory, choose
some other directory when you install python from python.org.

-- Leo
 
S

Sachin Punjabi

modules like utf-8 codec.

python distribution alone. Do not remove it, do not upgrade it. Some
programs provided by the manufacturer can stop working. If the
preinstalled python was installed into c:\python24 directory, choose
some other directory when you install python from python.org.

-- Leo

I installed it again but it makes no difference. It still throws me
error for LookUp Error: unknown encoding : utf-8.

Sachin
 
L

Leo Kislov

Sachin said:
I installed it again but it makes no difference. It still throws me
error for LookUp Error: unknown encoding : utf-8.

Most likely you're not using the new python, you're still running old
one.

-- Leo
 
S

Sachin Punjabi

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.

"rb" (it will be set to binary mode behind your back regardless ;-)

hth,
-- bjorn

I tried with the code you specified on the command line and it works
very much fine.

Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
Sachin.
 
S

Sachin Punjabi

one.

-- Leo

I installed the newer version on D drive and it was previously
installed on C drive. Also the command which bjorn asked me to execute
on command line worked very much fine.

Sachin.
 
F

Fredrik Lundh

Sachin said:
I installed the newer version on D drive and it was previously
installed on C drive. Also the command which bjorn asked me to execute
on command line worked very much fine.

what happens if you *type* in the problematic statements at the command
line, e.g.

if this still gives you the same exception, what output do you do the
same in a Python interpreter run with the "-v" option:
> d:
> cd \python24
> python -v ....

</F>
 
S

Sachin Punjabi

one.

-- Leo

Actually, I have placed the exe created from python in seperate folder
other than Python root folder. Is this the cause of the problem but I
had set Path to C:\Python24\ in Environment Variables.

Sachin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top