unicode string problems

Gonçalo Rodrigues op73418 at mail.telepac.pt
Mon Apr 1 18:50:59 EST 2002


On 02 Apr 2002 01:24:52 +0200, martin at v.loewis.de (Martin v. Loewis)
wrote:

>Gonçalo Rodrigues <op73418 at mail.telepac.pt> writes:
>
>> My problem is the following:
>> 
>> Let f be a file object. If I do
>> 
>> f.write("Março 2002")
>> 
>> all goes well. But if I try
>
>It depends on your meaning of "well". It writes 9 bytes into the file,
>yes.

All goes well = it magically works without me having to give a second
thought of encodings ;-)

>
>> f.write("Março 2002" + march.Name())
>> 
>> where march.Name() returns a unicode string I get a unicode error. I
>> tried converting both unicodes to strings via str but obviously I got an
>> error (in the first string the culprit is the "ç" character).
>> 
>> Can someone help me out here and show me the way to write these strings
>> to the file?
>
>If you want to write text into a file, you should know what encoding
>you want to use in this file. Then make sure that all byte strings
>written to the file indeed use that encoding.
>
>In the specific case,
>
>f.write("Março 2002" + march.Name().encode("iso-8859-1"))
>
>will do the right thing, assuming you want the text to be encoded in
>that encoding.

Thanks, this should solve it. Now back to coding (gonna be a long
night...)

>
>HTH,
>Martin

All the best,
Gonçalo Rodrigues




More information about the Python-list mailing list