Re: string u'hyv\xe4' to file as 'hyvä'

Mark Tolonen metolone+gmane at gmail.com
Mon Dec 27 01:47:58 EST 2010


"gintare" <g.statkute at gmail.com> wrote in message 
news:83dc3076-9ddc-42bd-8c33-6af96b2634ba at l32g2000yqc.googlegroups.com...
> Hello,
> STILL do not work. WHAT to be done.
>
> import codecs
> item=u'hyv\xe4'
> F=codecs.open('/opt/finnish.txt', 'w+', 'utf8')
> F.writelines(item.encode('utf8'))
> F.close()
>
> In file i find 'hyv\xe4' instead of hyvä.

When you open a file with codecs.open(), it expects Unicode strings to be 
written to the file.  Don't encode them again.  Also, .writelines() expects 
a list of strings.  Use .write():

    import codecs
    item=u'hyv\xe4'
    F=codecs.open('/opt/finnish.txt', 'w+', 'utf8')
    F.write(item)
    F.close()

An additional comment, if you save the script in UTF8, you can inform Python 
of that fact with a special comment, and actually use the correct characters 
in your string constants (ä instead of \xe4).  Make sure to use a text 
editor that can save in UTF8, or use the correct coding comment for whatever 
encoding in which you save the file.

    # coding: utf8
    import codecs
    item=u'hyvä'
    F=codecs.open('finnish.txt', 'w+', 'utf8')
    F.write(item)
    F.close()

-Mark 





More information about the Python-list mailing list