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

MRAB python at mrabarnett.plus.com
Sun Dec 26 18:14:06 EST 2010


On 26/12/2010 22:43, gintare wrote:
> Could you please help me with special characters saving to file.
>
> I need to write the string u'hyv\xe4' to file.
> I would like to open file and to have line 'hyvä'
>
> import codecs
> word= u'hyv\xe4'
> F=codecs.open(/opt/finnish.txt, 'w+','Latin-1')

This opens the file using the Latin-1 encoding (although only if you
put the filename in quotes).
>
> F.writelines(item.encode('Latin-1'))

This encodes the Unicode item (did you mean 'word'?) to a bytestring
using the Latin-1 encoding. You opened the file using Latin-1 encoding,
so this is pointless. You should pass a Unicode string; it will encode
it for you.

You're also passing a bytestring to the .writelines method, which
expects a list of strings.

What you should be doing is this:

     F.write(word)

> F.writelines(item.encode('utf8'))

This encodes the Unicode item to a bytestring using the UTF-8 encoding.
This is also pointless. You shouldn't be encoding to UTF-8 and then
trying to write it to a file which was opened using Latin-1 encoding!

> F.writelines(item)
>
> F.close()
>
> All three writelines gives the same result in finnish.txt:   hyv\xe4
> i would like to find 'hyvä'.
>



More information about the Python-list mailing list