打印UTF

编程入门 行业动态 更新时间:2024-10-28 16:22:27
打印UTF-8编码的字节串(Printing UTF-8-encoded byte string)

我有一个表格的数据: v = "\xc5\x84"

这是utf-8编码字符“ń”的字节表示。

如何使用变量v打印>>ń<<

我正在使用python 2.7.2

原来变量v包含字符串:

v = "\\xc5\\x84" (双反斜杠)

VS

v = "\xc5\x84" (单个反斜杠)

这本身就是有效的utf-8字符。

I have a data of a form: v = "\xc5\x84"

This is a byte representation of an utf-8 encoded character "ń".

How can I print >>ń<< using variable v?

I'm using python 2.7.2

In original the variable v contained string:

v = "\\xc5\\x84" (double backslashes)

vs

v = "\xc5\x84" (single backslashes)

which is by itself valid utf-8 character.

最满意答案

编辑在我的机器中,输出取决于使用的shell / python,如下所示。 正如Klaus评论的那样,这里的主要角色将是您系统中的区域设置。

>>> v = "\xc5\x84" >>> print v #in pycrust shell python 2.6 Å„ >>> >>> print (v) #in idle python 3.2 Å >>>

机器具有以下设置:

>>> import locale >>> locale.getlocale() ('es_ES', 'cp1252')

独立于此设置,您可以获得角色

>>> print v.decode('utf-8') ń >>>

Edit In my machine the output depends on the shell/python used, as shown below. As commented by Klaus a major actor here would be the locale setting in your system.

>>> v = "\xc5\x84" >>> print v #in pycrust shell python 2.6 Å„ >>> >>> print (v) #in idle python 3.2 Å >>>

the machine has the following settings:

>>> import locale >>> locale.getlocale() ('es_ES', 'cp1252')

Independently of this setting, you get your character with

>>> print v.decode('utf-8') ń >>>

更多推荐

本文发布于:2023-07-04 16:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UTF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!