Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python : working with german umlaut

months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]

print months[2].decode("utf-8")

Printing month[2] fails with

UnicodeDecodeError: 'utf8' codec can't decode bytes in position 1-2: invalid data

help to get rid of this!

like image 236
deimus Avatar asked Aug 31 '11 07:08

deimus


2 Answers

Did you add an encoding in the begining of your source file ?

# -*- coding: utf-8 -*-
like image 196
Cédric Julien Avatar answered Oct 25 '22 01:10

Cédric Julien


Are you sure you are working in UTF8?

Nevertheless, I would recommend to define

months = [u"Januar", u"Februar", u"März", u"April", u"Mai", u"Juni",
          u"Juli", u"August", u"September", u"Oktober", u"November", u"Dezember"]

print months[2]

and if you do some stuff basing on bytes, you encode() the data into the charset you need.

like image 5
glglgl Avatar answered Oct 25 '22 02:10

glglgl