Python - 如何将诸如 '\xc3\xb6' 之类的 utf 文字转换为字母 ö [英] Python - How to convert utf literal such as '\xc3\xb6' to the letter ö

查看:39
本文介绍了Python - 如何将诸如 '\xc3\xb6' 之类的 utf 文字转换为字母 ö的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有德语 Umlaute 的编码 url 转换为带有这些 Umlaute 的字符串.

以下是编码字符串 = 'K%C3%B6nnen' 的示例.我想将其转换为Können"

当我使用 urllib.unquote(a) 我得到这个返回:'K\xc3\xb6nnen'

\xc3\xb6 我发现是 utf 文字.

如何将其转换为 ö ?我发现如果我使用打印函数它会正确转换它,但我不知道如何让函数返回这个值?有什么想法吗?

解决方案

With decode("utf-8")

print('K\xc3\xb6nnen'.decode("utf-8"))

输出

Können

额外编辑,请注意

<预><代码>>>>l = []>>>l.append(s.decode("utf-8")) #s 是字符串>>>升[u'K\xf6nnen']>>>打印(升)[u'K\xf6nnen']>>>打印(l [0])科嫩>>>

Python 将使用编码来管理字符串,print 可以为您提供表示但没有实际值,使用 repr(s) 为实际值

I am trying to convert an encoded url with german Umlaute into a string with those Umlaute.

Here is an example of an encoded string = 'K%C3%B6nnen'. I would like to convert it to 'Können'

When I use urllib.unquote(a) I get this returned: 'K\xc3\xb6nnen'

\xc3\xb6 I found out is a utf literal.

How can I convert this to an ö ? I find that if I use the print function it converts it correctly, but I cannot figure out how to get a function to return this value? Any ideas?

解决方案

With decode("utf-8")

print('K\xc3\xb6nnen'.decode("utf-8"))

OUTPUT

Können

EXTRA edit, take care with that

>>> l = []
>>> l.append(s.decode("utf-8")) #s is the string
>>> l
[u'K\xf6nnen']
>>> print(l)
[u'K\xf6nnen']
>>> print(l[0])
Können
>>> 

Python will use codification to manage string, print can give you the representation but no the real value, use repr(s) for real value

这篇关于Python - 如何将诸如 '\xc3\xb6' 之类的 utf 文字转换为字母 ö的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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