如何从Python中的发电机表达式打印Unicode?
创建来自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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Unicode是字节编码的字符,而不是逃脱序列。 python 3字符串是Unicode。要返回与Unicode代码点相对应的字符使用 noreflow noreferrer“> chr :
要生成2663至2670之间的字符:
逃脱序列使用十六进制符号。 0x2663是小数为9827,而0x2670变为9840。
您也可以使用HEX数字文字:
的问题完全相同的逻辑
或者,使用与原始代码不起作用 ,是用来代表单个逃生序列的问题当我们不能或不想键入字符本身时,字符串中的字符。解释器或编译器用相应的角色即时替代它们。字符串
\\ u26
是一个逃逸\
,其次是u
,2
和6
6 ::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 :
To generate the characters between 2663 and 2670:
Escape sequences use hexadecimal notation though. 0x2663 is 9827 in decimal, and 0x2670 becomes 9840.
You can use also use hex numeric literals:
or, to use exactly the same logic as the question
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 byu
,2
and6
: