Writing UTF-8 string to UNICODE file

Harvey Thomas hst at empolis.co.uk
Tue Nov 11 12:57:55 EST 2003


Michael Weir wrote
> 
> I'm sure this is a very simple thing to do, once you know how 
> to do it, but
> I am having no fun at all trying to write utf-8 strings to a 
> unicode file.
> Does anyone have a couple of lines of code that
> - opens a file appropriately for output
> - writes to this file
> Thanks very much.
> Michael Weir


Are your strings Unicode strings? If they are, then write them as UTF-8 encoded strings by

import codecs
f = codecs.open('myfile', 'w', 'utf8')
f.write(u'Unicode string')

HTH

Harvey

_____________________________________________________________________
This message has been checked for all known viruses by the MessageLabs Virus Scanning Service.





More information about the Python-list mailing list