如何在Python的curses库中使用扩展字符?
我一直在阅读有关 Python 中的 Curses 编程的教程,许多教程提到了使用扩展字符的能力,例如画线符号。 他们是人物> 255,curses 库知道如何以当前终端字体显示它们。
有些教程说你像这样使用它:
c = ACS_ULCORNER
...有些人说你像这样使用它:(
c = curses.ACS_ULCORNER
这应该是一个盒子的左上角,就像一个垂直翻转的L)
无论如何,无论我使用哪种方法使用时,名称未定义,因此程序失败。 我尝试了“导入诅咒”和“从诅咒导入*”,但都不起作用。
Curses 的 window() 函数使用了这些字符,所以我什至尝试在我的盒子上查找源代码,看看它是如何实现的,但我在任何地方都找不到它。
I've been reading tutorials about Curses programming in Python, and many refer to an ability to use extended characters, such as line-drawing symbols. They're characters > 255, and the curses library knows how to display them in the current terminal font.
Some of the tutorials say you use it like this:
c = ACS_ULCORNER
...and some say you use it like this:
c = curses.ACS_ULCORNER
(That's supposed to be the upper-left corner of a box, like an L flipped vertically)
Anyway, regardless of which method I use, the name is not defined and the program thus fails. I tried "import curses" and "from curses import *", and neither works.
Curses' window() function makes use of these characters, so I even tried poking around on my box for the source to see how it does it, but I can't find it anywhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须将本地设置为 all,然后将输出编码为 utf-8,如下所示:
输出:
あ
you have to set your local to all, then encode your output as utf-8 as follows:
output:
あ
来自curses/__init__.py:
换句话说:
From
curses/__init__.py
:In other words:
我相信以下内容是适当相关的,将在这个问题下发布。 在这里我将使用 utfinfo.pl (另请参阅 超级用户)。
首先,对于标准 ASCII 字符集,Unicode 代码点和字节编码是相同的:
所以我们可以在 Python 的
curses
中执行以下操作:... 并且它按预期工作
但是,如果一个字符高于基本 ASCII,则存在差异,其中
addch
文档 不一定明确。 首先,我可以这样做:...在这种情况下,在我的
gnome-terminal
中,将呈现 Unicode 字符“π”。 但是,如果您检查ACS_PI
,您会看到它是一个整数,值为 4194427 (0x40007b); 因此,以下内容也会呈现相同的字符(或评分者、字形?)“π”:为了查看发生了什么,我搜索了
ncurses
源代码,并发现了以下内容:此处注意:
..两者都与
ACS_PI
的 4194427 (0x40007b) 值无关。因此,当
addch
和/或border
看到 ASCII 上方的字符(基本上是unsigned int
,而不是unsigned char
code>),他们(至少在这种情况下)使用该数字不作为 Unicode 代码点,或作为 UTF-8 编码字节表示 - 相反,他们使用它作为查找索引acs_map
-ping 函数(不过,最终会返回 Unicode 代码点,即使它模拟 VT-100)。 这就是为什么以下规范:在 Python 2.7 中会失败,
参数 1 或 3 必须是 ch 或 int
; 在 Python 3.2 中会简单地渲染一个空格而不是一个字符。 当我们指定'π'
时。 我们实际上已经指定了 UTF-8 编码 [0xCF,0x80] - 但即使我们指定了 Unicode 代码点:...它在 Python 2.7 和 3.2 中也不会渲染任何内容(空格)。
话虽这么说 - 函数
addstr
确实 接受 UTF-8 编码的字符串,并且工作正常:...但是对于边框 - 因为
border()
显然以与addch()
相同的方式处理字符 - 我们显然运气不好,对于任何未明确指定为ACS
常量的内容(并且没有那么多)其中之一)。希望这对某人有帮助,
干杯!
I believe the below is appropriately related, to be posted under this question. Here I'll be using utfinfo.pl (see also on Super User).
First of all, for standard ASCII character set, the Unicode code point and the byte encoding is the same:
So we can do in Python's
curses
:... and it works as intended
However, if a character is above basic ASCII, then there are differences, which
addch
docs don't necessarily make explicit. First, I can do:... in which case, in my
gnome-terminal
, the Unicode character 'π' is rendered. However, if you inspectACS_PI
, you'll see it's an integer number, with a value of 4194427 (0x40007b); so the following will also render the same character (or rater, glyph?) 'π':To see what's going on, I grepped through the
ncurses
source, and found the following:Note here:
... neither of which relates to the value of 4194427 (0x40007b) for
ACS_PI
.Thus, when
addch
and/orborder
see a character above ASCII (basically anunsigned int
, as opposed tounsigned char
), they (at least in this instance) use that number not as Unicode code point, or as UTF-8 encoded bytes representation - but instead, they use it as a look-up index foracs_map
-ping function (which ultimately, however, would return the Unicode code point, even if it emulates VT-100). That is why the following specification:will fail in Python 2.7 with
argument 1 or 3 must be a ch or an int
; and in Python 3.2 would render simply a space instead of a character. When we specify'π'
. we've actually specified the UTF-8 encoding [0xCF,0x80] - but even if we specify the Unicode code point:... it simply renders nothing (space) in both Python 2.7 and 3.2.
That being said - the function
addstr
does accept UTF-8 encoded strings, and works fine:... but for borders - since
border()
apparently handles characters in the same wayaddch()
does - we're apparently out of luck, for anything not explicitly specified as anACS
constant (and there's not that many of them, either).Hope this helps someone,
Cheers!