Graham Dumpleton
grahamd at dscpl.com.au
Sun Jun 25 08:11:09 EDT 2006
On 25/06/2006, at 10:10 PM, b3nder wrote: > Hi, list. > > Tell me please how to use utf-8 in ouput? > > from mod_python import apache > import codecs > import os > > > def handler(req): > req.content_type = 'text/plain; charset=utf-8' > req.send_http_header() > file = codecs.open( > os.path.dirname(__file__) + "/myfile.txt", "r", "utf-8") > a = file.read() > req.write(a) > > return apache.OK > > > gives error UnicodeEncodeError: 'ascii' codec can't encode > characters in position 0-5: ordinal not in range(128) Explicitly convert the Unicode string to a byte string before writing it out. req.write(a.encode('utf-8')) Play with it at Python command line prompt by doing: >>> a=u"\u1234" >>> a.encode('utf-8') '\xe1\x88\xb4' Graham
|