Writing UTF-8 string to UNICODE file

Alan Kennedy alanmk at hotmail.com
Tue Nov 11 13:05:31 EST 2003


Michael Weir wrote:
> Does anyone have a couple of lines of code that
> - opens a file appropriately for output
> - writes to this file

Simplest way (IMHO), with python 2.3

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import codecs

f = codecs.open('myunicodefile.txt', 'wt', 'utf-8')
for i in range(5):
    for j in range(32, 300):
        f.write(unichr(j))
    f.write('\n')
f.close()
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

HTH,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list