Python unicode problem

Posted by Somebody still uses you MS-DOS on Stack Overflow See other posts from Stack Overflow or by Somebody still uses you MS-DOS
Published on 2010-05-27T20:47:58Z Indexed on 2010/05/27 21:51 UTC
Read the original article Hit count: 337

Filed under:
|
|

I'm receiving some data from a ZODB (Zope Object Database). I receive a mybrains object. Then I do:

o = mybrains.getObject()

and I receive a "Person" object in my project. Then, I can do

b = o.name

and doing print b on my class I get:

José Carlos

and print b.name.__class__

<type 'unicode'>

I have a lot of "Person" objects. They are added to a list.

names = [o.nome, o1.nome, o2.nome]

Then, I trying to create a text file with this data.

delimiter = ';'
all = delimiter.join(names) + '\n'

No problem. Now, when I do a print all I have:

José Carlos;Jonas;Natália
Juan;John

But when I try to create a file of it:

f = open("/tmp/test.txt", "w")
f.write(all)

I get an error like this (the positions aren't exaclty the same, since I change the names)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 84: ordinal not in range(128)

If I can print already with the "correct" form to display it, why I can't write a file with it? Which encode/decode method should I use to write a file with this data?

I'm using Python 2.4.5 (can't upgrade it)

© Stack Overflow or respective owner

Related posts about python

Related posts about unicode