Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python UnicodeDecodeError when writing German letters

I've been banging my head on this error for some time now and I can't seem to find a solution anywhere on SO, even though there are similar questions.

Here's my code:

f = codecs.open(path, "a", encoding="utf-8")
value = "Bitte überprüfen"
f.write(("\"%s\" = \"%s\";\n" % ("no_internet", value)).encode("utf-8"))

And what I get as en error is:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128)

Why ascii if I say utf-8? I would really appreciate any help.

like image 541
Banana Avatar asked Mar 04 '15 10:03

Banana


1 Answers

Try:

value = u"Bitte überprüfen"

in order to declare value as a unicode string and

# -*- coding: utf-8 -*-

at the start of your file in order to declare that your python file is saved with utf-8 encoding.

like image 76
JuniorCompressor Avatar answered Oct 20 '22 12:10

JuniorCompressor