macos Python:如何在 Mac 中使用 ASCII #219 打印“块”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10635226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: How to print a 'block' using ASCII #219 in Mac
提问by andio
I use python on Mac OS X, and I want to use an extended ASCII symbol #219 like in this table :
我在 Mac OS X 上使用 python,我想使用扩展 ASCII 符号 #219,如下表所示:
https://theasciicode.com.ar/extended-ascii-code/block-graphic-character-ascii-code-219.html
https://theasciicode.com.ar/extended-ascii-code/block-graphic-character-ascii-code-219.html
The problem, I found out that 'block' character doesn't exist in Mac ASCII... I'm not sure.
问题是,我发现 Mac ASCII 中不存在“块”字符……我不确定。
Can anyone help me? I was trying to print using unichr(219)
, but it was giving me different result. It will output ?
. What i want is █
谁能帮我?我试图使用 打印unichr(219)
,但它给了我不同的结果。它会输出?
. 我想要的是█
回答by mata
the corresponding unicode character is 0x2588
, so use that:
对应的unicode 字符是0x2588
,所以使用:
print unichr(0x2588) # or:
print u"\u2588"
should give you the right result.
if you want it in a different encoding, you can always encode
it.
应该给你正确的结果。如果你想要不同的编码,你总是encode
可以的。
回答by ch3ka
The character you posted isn't 219, it's 9608. And printing it should work fine:
您发布的字符不是 219,而是 9608。打印它应该可以正常工作:
python3:
蟒蛇3:
>>> ord("█")
9608
>>> chr(9608)
'█'
python2:
蟒蛇2:
>>> unichr(9608)
u'\u2588'
>>> print(u'\u2588')
█
if you still have troubles, set your teminal to use utf-8 and .encode()
to utf-8 if neccessary.
如果您仍然遇到问题,请将您的终端设置为使用 utf-8 并.encode()
在必要时使用 utf-8 。
回答by Burhan Khalid
That character doesn't exist the Extended ASCII table for Mac, however the unicode representation will work.
该字符不存在Mac的扩展 ASCII 表,但是 unicode 表示将起作用。
回答by The Godfather
While the rest of the comments were correctly pointing out technical Python side, there is some details for ASCII explanation.
虽然其余的评论正确地指出了技术 Python 方面,但有一些 ASCII 解释的细节。
When you're using programming language, you almost always deal with Unicode, and the important thing is that Unicode encoding is different from ASCII. So you need to check Unicode table, like Wikior Unicode Table.
当您使用编程语言时,您几乎总是与 Unicode 打交道,重要的是 Unicode 编码与 ASCII 不同。所以你需要检查 Unicode 表,比如Wiki或Unicode Table。
What you saw in your code is ?
which is exactly the Unicode symbol 219 / 0xDB. And what you wanted is █
which is Unicode Block symbol 9608 / 0x2588
您在代码中看到的?
正是 Unicode 符号219 / 0xDB。你想要的█
是 Unicode 块符号9608 / 0x2588