Unicode conversion problem (codec can't decode)

M.-A. Lemburg mal at egenix.com
Fri Apr 4 06:19:45 EDT 2008


On 2008-04-04 08:18, Jason Scheirer wrote:
> On Apr 3, 9:35 pm, "Eric S. Johansson" <e... at harvee.org> wrote:
>> I'm having a problem (Python 2.4) converting strings with random 8-bit
>> characters into an escape form which is 7-bit clean for storage in a database.

If you don't want to process the 7-bit form in any way, there
are a couple of encodings which you could use:

>> Here's an example:
>>
>> body = meta['mini_body'].encode('unicode-escape')
>>
>> when given an 8-bit string, (in meta['mini_body']), the code fragment above
>> yields the error below.
>>
>> 'ascii' codec can't decode byte 0xe1 in position 13: ordinal not in range(128)

Try this:

body = meta['mini_body'].decode('latin-1').encode('unicode-escape')
mini_body = body.decode('unicode-escape').encode('latin-1')

or this:

body = meta['mini_body'].decode('latin-1').encode('utf-7')
mini_body = body.decode('utf-7').encode('latin-1')

If all you need is the 7-bit form, you're probably better of
with a base64 encoding:

body = meta['mini_body'].encode('base64')
mini_body = body.decode('base64')

>> the string that generates that error is:
>>
>> <br>Reduce Whát You Owe by 50%. Get out of debt today!<br>Reduuce Interest &
>> |V|onthlyy Paymeñts Easy, we will show you how..<br>Freee Quote in 10
>> Min.<br>http://www.freefromdebtin.net.cn

Looks like spam :-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Apr 04 2008)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
            Registered at Amtsgericht Duesseldorf: HRB 46611



More information about the Python-list mailing list