Re: string u'hyv\xe4' to file as 'hyvä'
- From: "Mark Tolonen" <metolone+gmane@xxxxxxxxx>
- Date: Sun, 26 Dec 2010 22:47:58 -0800
"gintare" <g.statkute@xxxxxxxxx> wrote in message news:83dc3076-9ddc-42bd-8c33-6af96b2634ba@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: string u'hyv\xe4' to file as 'hyvä'
- From: Alex Willmer
- Re: string u'hyv\xe4' to file as 'hyvä'
- References:
- string u'hyv\xe4' to file as 'hyvä'
- From: gintare
- Re: string u'hyv\xe4' to file as 'hyvä'
- From: gintare
- string u'hyv\xe4' to file as 'hyvä'
- Prev by Date: Re: string u'hyv\xe4' to file as 'hyvä'
- Next by Date: Re: User input masks - Access Style
- Previous by thread: Re: string u'hyv\xe4' to file as 'hyvä'
- Next by thread: Re: string u'hyv\xe4' to file as 'hyvä'
- Index(es):