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

MRAB python at mrabarnett.plus.com
Mon Dec 27 12:22:06 EST 2010


On 27/12/2010 05:56, gintare wrote:
> 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()

As I said in my previous post, you shouldn't be using .writelines, and
you shouldn't encode it when writing it to the file because codecs.open
will do that for you, that's its purpose:

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

>
> In file i find 'hyv\xe4' instead of hyvä.
>
> Sorry for mistyping in previous letter about 'latin-1'. I was making
> all possible combinations, when normal example syntax did not work,
> before writting to this forum
>
> regards,
> gintare
>
>
>
> On 27 Gruo, 01:14, MRAB<pyt... at mrabarnett.plus.com>  wrote:
>> 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 '.- Slėpti cituojamą tekstą -
>>
>> - Rodyti cituojamą tekstą -
>
>




More information about the Python-list mailing list