Python UnicodeDecodeError时写德文字母 [英] Python UnicodeDecodeError when writing German letters

查看:140
本文介绍了Python UnicodeDecodeError时写德文字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个错误中一直在打扰我一段时间,尽管有类似的问题,我似乎也找不到任何地方的解决方案。

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.

这是我的代码:

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

我发现en错误是:

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

为什么ascii如果我说utf-8?我真的很感谢任何帮助。

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

推荐答案

尝试:

value = u"Bitte überprüfen"

为了将值声明为一个unicode字符串和

in order to declare value as a unicode string and

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

在您的文件的开头,以声明您的python文件使用utf-8编码保存。

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

这篇关于Python UnicodeDecodeError时写德文字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆