Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unicode codecs

Reply
Thread Tools

unicode codecs

 
 
Ivan Voras
Guest
Posts: n/a
 
      02-09-2004
When concatenating strings (actually, a constant and a string...) i get
the following error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 1:
ordinal not in range(12

Now I don't think either string is unicode, but I'm working with
win32api so it might be... The point is: I know all values will fit
in a particular code page (iso-8859-2), so how do I change the 'ascii'
codec in the above error into something that will work?

 
Reply With Quote
 
 
 
 
Christopher Koppler
Guest
Posts: n/a
 
      02-09-2004
On Mon, 09 Feb 2004 21:59:36 +0100, Ivan Voras
<ivoras@__geri.cc.fer.hr> wrote:

>When concatenating strings (actually, a constant and a string...) i get
>the following error:
>
>UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 1:
>ordinal not in range(12
>
>Now I don't think either string is unicode, but I'm working with
>win32api so it might be... The point is: I know all values will fit
>in a particular code page (iso-8859-2), so how do I change the 'ascii'
>codec in the above error into something that will work?


To get a real solution, you should also post the offending code, but
you might try to convert your values to unicode with the built-in
unicode() and the string method decode(). See the library reference
sections 2.1 and 2.2.6.

--
Christopher
 
Reply With Quote
 
 
 
 
Ivan Voras
Guest
Posts: n/a
 
      02-09-2004
Christopher Koppler wrote:

> To get a real solution, you should also post the offending code, but
> you might try to convert your values to unicode with the built-in
> unicode() and the string method decode(). See the library reference
> sections 2.1 and 2.2.6.


I tried that, without luck. It is somewhat difficult to reproduce the
problem, but here's how I see it:

- win32api function returns a string (8bit) with some of the characters
from the upper half of code page, let's call it s1
- a statement such as a='x'+s1 fails with the above error.

I don't really know why should concatenation check if characters are
7-bit clean (or indeed if they represent anything in whatever code page).

Since win32api functions exist also in unicode version, I tried this:

- call the unicode version of function. Returned is a unicode string
(checked, it really is unicode) like u'R\xfcgenwald.txt', let's call it s2
- a statement a='x'+s2.encode('iso-8859-2') also fails with the exact
same error.

It is strange that if I execute similar code in Idle (e.g. manually
assigning string constants to variables and concatenating), everything
works!

The exact error is:
File "E:\develop\pynetdb\netdbcreate.py", line 32, in walkdirs
fullname = root+'\\'+filename
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1:
ordinal not in range(12

The filename variable contains (in my latest effort) utf-8 encoded value
'R\xc3\xbcgenwald.mp3', and root variable contains a normal non-unicode
string.

I tried various combinations of unicode and non-unicode types, and thay
all fail sooner or later when they meet with a non-unicode string that
is not 7-bit clean.
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      02-09-2004
Ivan Voras wrote:
> When concatenating strings (actually, a constant and a string...) i get
> the following error:
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 1:
> ordinal not in range(12
>
> Now I don't think either string is unicode


This statement must be false. When concatenating two byte strings, no
codec is ever used. So, either
1. one of the strings is a Unicode objects, or
2. you are not performing concatenation, or you get the exception
from an operation that is not concatenation, or
3. you are not getting this exception.

Most likely, it is 1)

> The point is: I know all values will fit
> in a particular code page (iso-8859-2), so how do I change the 'ascii'
> codec in the above error into something that will work?


Explicitly encode the Unicode string in your concatenation as
iso-8859-2.

Regards,
Martin

 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      02-09-2004
Ivan Voras wrote:
> - win32api function returns a string (8bit) with some of the characters
> from the upper half of code page, let's call it s1


Are you absolutely certain that type(s1) is str?

> - a statement such as a='x'+s1 fails with the above error.


Are you absolutely certain the constant is the literal string 'x'?

> I don't really know why should concatenation check if characters are
> 7-bit clean (or indeed if they represent anything in whatever code page).


As you have shown, there would be no need, and indeed, Python will not
check code pages in this case. So you must be doing something else.

> - call the unicode version of function. Returned is a unicode string
> (checked, it really is unicode) like u'R\xfcgenwald.txt', let's call it s2
> - a statement a='x'+s2.encode('iso-8859-2') also fails with the exact
> same error.


How do you know it is the concatenation that causes the exception?

> The exact error is:
> File "E:\develop\pynetdb\netdbcreate.py", line 32, in walkdirs
> fullname = root+'\\'+filename
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1:
> ordinal not in range(12
>
> The filename variable contains (in my latest effort) utf-8 encoded value
> 'R\xc3\xbcgenwald.mp3', and root variable contains a normal non-unicode
> string.


Which string precisely (what is its repr())?

Regards,
Martin

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      02-09-2004
Ivan Voras wrote:

> When concatenating strings (actually, a constant and a string...) i get
> the following error:
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 1:
> ordinal not in range(12
>
> Now I don't think either string is unicode, but I'm working with
> win32api so it might be... The point is: I know all values will fit
> in a particular code page (iso-8859-2), so how do I change the 'ascii'
> codec in the above error into something that will work?


You can either convert all strings to unicode or to iso-8859-2.
A hands on approach:

>>> u,s

(u'R\xfcbe', 'R\xfcbe')
>>> u+s

Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 1:
ordinal not in range(12

This error is prevented by an explicit conversion:

>>> u.encode("iso-8859-1") + s

'R\xfcbeR\xfcbe'

or

>>> u + s.decode("iso-8859-1")

u'R\xfcbeR\xfcbe'

If you aren't sure which string is unicode and which is not:

>>> def toiso(s):

.... if isinstance(s, unicode):
.... return u.encode("iso-8859-1")
.... return s
....
>>> toiso(u) + toiso(s)

'R\xfcbeR\xfcbe'

Peter


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      02-09-2004
Peter Otten wrote:

>>>> def toiso(s):

> ... if isinstance(s, unicode):
> ... return u.encode("iso-8859-1")
> ... return s
> ...
>>>> toiso(u) + toiso(s)

> 'R\xfcbeR\xfcbe'


Oops, that should be:

>>> def toiso(t):

.... if isinstance(t, unicode):
.... return t.encode("iso-8859-1")
.... return t
....
>>> toiso(u) + toiso(s)

'R\xfcbeR\xfcbe'

 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      02-09-2004
Martin v. Löwis wrote:
> Ivan Voras wrote:
>
>> - win32api function returns a string (8bit) with some of the
>> characters from the upper half of code page, let's call it s1

>
>
> Are you absolutely certain that type(s1) is str?


Yes. Plain string.

>> - a statement such as a='x'+s1 fails with the above error.

>
>
> Are you absolutely certain the constant is the literal string 'x'?


Um, what else could it be? This is an example, in the real case the
literal string is something else (but of the same format).


>> - call the unicode version of function. Returned is a unicode string
>> (checked, it really is unicode) like u'R\xfcgenwald.txt', let's call
>> it s2
>> - a statement a='x'+s2.encode('iso-8859-2') also fails with the exact
>> same error.

>
> How do you know it is the concatenation that causes the exception?


What else could cause it? It's a simple command, nothing fancy - an
concatenation and assignment.

I've tried converting everything to use unicode and I'm getting *really*
weird results now - it may be a bug in the win32api library.
 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      02-09-2004
Peter Otten wrote:

> You can either convert all strings to unicode or to iso-8859-2.
> A hands on approach:
>
>
>>>>u,s

>
> (u'R\xfcbe', 'R\xfcbe')
>
>>>>u+s

>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 1:
> ordinal not in range(12
>
> This error is prevented by an explicit conversion:


Thank you - I eventually found that out the hard way It was a mix of
some bugs from my code and the win32api library code, and I was seeing
exeptions pop up from both of them depending on what conditions were
met. Eventually I seem to have found a workaround for the library bugs
but I don't like it - it's a mixup of using unicode and code-page and
converting around when necessary. The good thing is that it doesn't seem
to influence performance a lot...

(Apparently, win32file.FindFilesW does something with its parameter that
breaks with above error when the parameter is unicode.)

Thanks for the help, all!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
codecs.register_error for "strict", unicode.encode() and str.decode() Alan Franzoni Python 0 07-26-2012 11:03 PM
Issues with `codecs.register` and `codecs.CodecInfo` objects Karl Knechtel Python 2 07-10-2012 02:49 PM
decode unicode string using 'unicode_escape' codecs aurora Python 2 01-14-2006 01:57 AM
singing the praises of unicode and codecs Steven Bethard Python 0 12-10-2004 06:43 PM
codecs latin1 unicode standard output file Marko Faldix Python 8 12-15-2003 09:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57