私有 Unicode 字符在 Python 3 解释器中的显示方式不同 [英] Private Unicode Character displays differently in Python 3 Interpreter

查看:34
本文介绍了私有 Unicode 字符在 Python 3 解释器中的显示方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 Windows 10 上使用 Private Character Editor 私下创建了一个 unicode 字符.该字符使用代码 E000 保存.我从字符映射中复制了它并粘贴到文本编辑器中,它起作用了.但是,当我将它粘贴到 Python IDLE 编辑器中时,它会更改为不同的 unicode 字符,甚至在运行程序之前也是如此.我不能使用 u'unicode_string' 或类似的东西,因为我的 unicode 字符甚至在解释器中都不起作用.我是编程新手.

我的问题是,如何在 Python 3.4 中使用我的私有 unicode 字符?

说明

我使用了 Windows 10 中的 Character Map 程序来复制符号并将其粘贴到我的 python 环境中.环境可能会在右侧截断它,因为它是一个宽字符并且环境似乎不喜欢那样.(我四处移动光标以使其呈现全角.)

然后我继续通过使用默认的 utf-8 编码对字符进行编码来获取代码点的十六进制转储,结果是 \xee\x80\xa1 作为 bytes 对象.

接下来,我将数据打印为字符串以向您显示一个常见错误,以及如果您尝试打印这些字节的字符串会打印什么.

然后,我打印了 b'\xee\x80\xa1',这就是您在软件中实际使用该符号的方式.

So I created a unicode character privately using Private Character Editor on Windows 10. The character was saved with the code E000. I copied it from the Character Map and pasted into a text editor and it worked. However, when I paste it into the Python IDLE editor it changes to a different unicode character, even before running the program. I can't use u'unicode_string' or anything like that because my unicode character doesn't even work in the interpreter. I am new to programming.

My question is, how do I use my private unicode character in Python 3.4?

This is what I see on Notepad.

This is what I see on Python 3.4 interpreter.

解决方案

Python isn't really the interesting part of this, rather the shell or terminal is. In our case, Windows uses special code points to represent private character encodings. To get those, you need to get a hex dump of the character on a shell in Windows, then you can render the character in Python.

NOTE: Use Unicode points E021 or higher, since lower number code points are usually used for control, and it seems that the Windows shell that the python interpreter and IDLE use doesn't let you override those with private characters.

Demonstration

I tested your issue by generating a private character of my own. I will put an image of my test here since it wouldn't be rendered properly in text here on Stack Overflow.

Explanation

I used the Character Map program in Windows 10 to copy the symbol and paste it into my python environment. The environment may truncate it on the right since it is a wide character and the environment didn't seem to like that. (I moved the cursor around to get it to render full-width.)

Then I proceeded to get the hexdump of the code point by encoding the character using the default utf-8 encoding, which turned out to be \xee\x80\xa1 as a bytes object.

Next I printed the data as a string to show you a common error, and what would be printed if you attempted to print a string of those bytes.

Then, I printed b'\xee\x80\xa1', which is how you would actually use the symbol in your software.

这篇关于私有 Unicode 字符在 Python 3 解释器中的显示方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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