[Tutor] UTF-8 by default

Kent Johnson kent37 at tds.net
Sun May 29 14:09:23 CEST 2005


Jonas Melian wrote:
> Kent Johnson wrote:
> 
>>>How write by default in UTF-8 instead of ASCII?
>>
>>
>>How are you writing the file? What encoding is your data originally?
>>
> 
> 
> For writing the XML file:
> 
>>>ElementTree.ElementTree(root).write("file.xml")

The elementTree.write() method takes an optional encoding parameter. For it to work, all the strings 
you put in your tree must be either ascii or unicode.

> The source encoding is UTF-8 ( # -*- coding: utf-8 -*- )

Any strings you put in the elementtree should be u"" strings.

> But I also want that the program shows its messages in differents
> languages, so it's necessary writting (>>>print 'Message') in UTF-8 by
> default

'Message' is already UTF-8; any 7-bit ASCII string is also a valid UTF-8 string. If you have 
non-ASCII strings you should encode them in the encoding of your terminal with
print u'Message'.encode('utf-8').

Kent



More information about the Tutor mailing list