Skip to content Skip to sidebar Skip to footer

Python Encoding Unicode Utf-8

I'm using selenium to insert text input with german umlauts in a web formular. The declared coding for the python script is utf-8. The page uses utf-8 encoding. When i definine a s

Solution 1:

This is probably bad encoding. Try printing q before the last statement, and see if it's equal. This line q = parser.get('info', 'query') # type(q) returns str should return the string 'H\xc3\xa4ll\xc3\xb6'. If it's different, then you are using the wrong coding.

>>> q = u"Hällö"# unicode obj>>> q
u'H\xe4ll\xf6'>>> print q
Hällö
>>> q.encode('utf-8')
'H\xc3\xa4ll\xc3\xb6'>>> a = q.encode('utf-8')  # str obj>>> a
'H\xc3\xa4ll\xc3\xb6'# <-- this should be the value of the str>>> a.decode('utf-8')  # <-- unicode obju'H\xe4ll\xf6'>>> print a.decode('utf-8')
Hällö
>>> 

Solution 2:

from ConfigParser import SafeConfigParser
import codecs

parser = SafeConfigParser()

with codecs.open('cfg.ini', 'r', encoding='utf-8-sig') as f:
    parser.readfp(f)
greet = parser.get('main', 'greet')

print'greet:', greet.encode('utf-8-sig')

greet: Hällö

cfg.ini file

[main]greet=Hällö

Post a Comment for "Python Encoding Unicode Utf-8"