[Chicago] understanding unicode problems

Feihong Hsu hsu.feihong at yahoo.com
Thu Nov 15 23:52:26 CET 2007


If there's a command line version of your app, try printing out the type of the name string you're passing to Reportlab. Or if it's django, add the lines

    strType = type(name)
    assert 0

and you'll get the debug page where you can inspect the value of strType.

If you see <type 'unicode'>, that's bad. You need to pass name.encode('utf-8') into Reportlab.

If you see <type 'str'>, that might or might not be bad. You can use the chardet library to figure out what encoding that string is in. For example:

>>> chardet.detect(name)
{'encoding': 'latin-1', 'confidence': 0.99}

In this example chardet tells you that the name string variable currently holds a string encoded in latin-1. To convert the string to utf-8, try this:

>>> name.decode('latin-1').encode('utf-8')

This takes the name string, converts it from latin-1 to a unicode string, then converts the unicode string to a normal string encoded in utf-8. 

Reportlab should happily generate the file for you once you give it the utf-8 string.

- Feihong

Procrastination Method #3: Solve everybody else's problems instead of your own.

Carl Karsten <carl at personnelware.com> wrote: of course now a unicode problem just hit me.

i use the  django admin to enter  Ivan Krstic'
and reportlab spits out: http://dev.personnelware.com/carl/a/IvanK1.pdf

so pretty much 100% python.

I am told:

 > Make sure that you are using utf-8 and not some other encoding, such as
 > latin-1.

But I really don't know what that means, nor do I even know how to debug this.

Carl K
_______________________________________________
Chicago mailing list
Chicago at python.org
http://mail.python.org/mailman/listinfo/chicago


       
---------------------------------
Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20071115/a7ee621a/attachment-0001.htm 


More information about the Chicago mailing list