doniyor
doniyor

Reputation: 37904

python write german umlaute into a file

I know, this question has been asked million times. But I am still stuck with it. I am using python 2 and cannot change to python 3.

problem is this:

>>> w = u"ümlaut"
>>> w
>>> u'\xfcmlaut'
>>> print w
ümlaut
>>> dic = {'key': w}
>>> dic
{'key': u'\xfcmlaut'}
>>> f = io.open('testtt.sql', mode='a', encoding='UTF8')
>>> f.write(u'%s' % dic)

then file has:

{'key': u'\xfcmlaut'}

I need {'key': 'ümlaut'} or {'key': u'ümlaut'}

What am I missing, I am still noob in encoding decoding things :/

Upvotes: 4

Views: 2083

Answers (2)

hspandher
hspandher

Reputation: 16753

The easiest solution is to switch to python3, but since you can't do that, how about converting dictionary to json first before trying to save it into the file.

import io
import json
import sys

reload(sys)
sys.setdefaultencoding('utf8')

w = u"ümlaut"
dic = {'key': w}
f = io.open('testtt.sql', mode='a', encoding='utf8')
f.write(unicode(json.dumps(dic, ensure_ascii=False).encode('utf8')))

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599866

I'm not sure why you want this format particularly, since it won't be valid to read into any other application, but never mind.

The problem is that to write a dictionary to a file, it needs to be converted to a string - and to do that, Python calls repr on all its elements.

If you create the output manually as a string, all is well:

d = "{'key': '%s'}" % w
with io.open('testtt.sql', mode='a', encoding='UTF8') as f:
    f.write(d)

Upvotes: 2

Related Questions