如何从Python中的发电机表达式打印Unicode?

发布于 2025-01-28 12:17:00 字数 791 浏览 3 评论 0原文

创建来自Generator表达式的列表:

V = [('\\u26' + str(x)) for x  in range(63,70)]

第一个问题:如果您尝试仅使用“ \ u” + str(...),它立即给出解码器错误。似乎它在看到\ u时,而不是在准备好时,它试图立即解码。我试图通过双重斜击来解决这个问题。

其次,这创造了一些有希望的东西,但仍然无法实际将它们打印为Unicode的控制器:

>>> print([v[0:] for v in V])
['\\u2663', '\\u2664', '\\u2665', .....]
>>> print(V[0])    
\u2663

我希望看到的是与使用'\ u0123'之类的命令相同的符号列表,例如:

>>> print('\u2663')
♣

有什么方法可以从生成的列表中做到这一点?还是有更好的方法可以打印它们而不是'\ u0123'格式?

这不是我想要的。我想查看绘制的实际符号,而不是Unicode值:

>>> print(['{}'.format(v[0:]) for v in V])
['\\u2663', '\\u2664', '\\u2665', '\\u2666', '\\u2667', '\\u2668', '\\u2669']

Create a list from generator expression:

V = [('\\u26' + str(x)) for x  in range(63,70)]

First issue: if you try to use just "\u" + str(...) it gives a decoder error right away. Seems like it tries to decode immediately upon seeing the \u instead of when a full chunk is ready. I am trying to work around that with double backslash.

Second, that creates something promising but still cannot actually print them as unicode to console:

>>> print([v[0:] for v in V])
['\\u2663', '\\u2664', '\\u2665', .....]
>>> print(V[0])    
\u2663

What I would expect to see is a list of symbols that look identical to when using commands like '\u0123' such as:

>>> print('\u2663')
♣

Any way to do that from a generated list? Or is there a better way to print them instead of the '\u0123' format?

This is NOT what I want. I want to see the actual symbols drawn, not the Unicode values:

>>> print(['{}'.format(v[0:]) for v in V])
['\\u2663', '\\u2664', '\\u2665', '\\u2666', '\\u2667', '\\u2668', '\\u2669']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-02-04 12:17:01

Unicode是字节编码的字符,而不是逃脱序列。 python 3字符串是Unicode。要返回与Unicode代码点相对应的字符使用 noreflow noreferrer“> chr

chr(i)
返回代表一个字符的字符串,其unicode代码点是整数i。例如,Chr(97)返回字符串“ A”,而Chr(8364)返回字符串'€'。这是ord()的倒数。

该参数的有效范围是从0到1,114,111(基数16中的0x10ffff)。如果我不在那个范围之外,将提高ValueError。

要生成2663至2670之间的字符:

>>> [chr(x) for x  in range(2663,2670)]
['੧', '੨', '੩', '੪', '੫', '੬', '੭']

逃脱序列使用十六进制符号。 0x2663是小数为9827,而0x2670变为9840。

>>> [chr(x) for x  in range(9827,9840)]
['♣', '♤', '♥', '♦', '♧', '♨', '♩', '♪', '♫', '♬', '♭', '♮', '♯']

您也可以使用HEX数字文字:

>>> [chr(x) for x  in range(0x2663,0x2670)]
['♣', '♤', '♥', '♦', '♧', '♨', '♩', '♪', '♫', '♬', '♭', '♮', '♯']

的问题完全相同的逻辑

>>> [chr(0x2600 + x) for x  in range(0x63,0x70)]
['♣', '♤', '♥', '♦', '♧', '♨', '♩', '♪', '♫', '♬', '♭', '♮', '♯']

或者,使用与原始代码不起作用 ,是用来代表单个逃生序列的问题当我们不能或不想键入字符本身时,字符串中的字符。解释器或编译器用相应的角色即时替代它们。字符串\\ u26是一个逃逸\,其次是u26 6 ::

>>> len('\\u26')
4

Unicode is a character to bytes encoding, not escape sequences. Python 3 strings are Unicode. To return the character that corresponds to a Unicode code point use chr :

chr(i)
Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string 'a', while chr(8364) returns the string '€'. This is the inverse of ord().

The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16). ValueError will be raised if i is outside that range.

To generate the characters between 2663 and 2670:

>>> [chr(x) for x  in range(2663,2670)]
['੧', '੨', '੩', '੪', '੫', '੬', '੭']

Escape sequences use hexadecimal notation though. 0x2663 is 9827 in decimal, and 0x2670 becomes 9840.

>>> [chr(x) for x  in range(9827,9840)]
['♣', '♤', '♥', '♦', '♧', '♨', '♩', '♪', '♫', '♬', '♭', '♮', '♯']

You can use also use hex numeric literals:

>>> [chr(x) for x  in range(0x2663,0x2670)]
['♣', '♤', '♥', '♦', '♧', '♨', '♩', '♪', '♫', '♬', '♭', '♮', '♯']

or, to use exactly the same logic as the question

>>> [chr(0x2600 + x) for x  in range(0x63,0x70)]
['♣', '♤', '♥', '♦', '♧', '♨', '♩', '♪', '♫', '♬', '♭', '♮', '♯']

The reason the original code doesn't work is that escape sequences are used to represent a single character in a string when we can't or don't want to type the character itself. The interpreter or compiler replaces them with the corresponding character immediatelly. The string \\u26 is an escaped \ followed by u, 2 and 6:

>>> len('\\u26')
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文