python - mintty 终端、python、curses 和 utf8 : incorrect display

标签 python curses

我问过here但我很确定我不会得到答案。

  • 复制-粘贴此 python 代码:
  • 执行它
  • 在经典的 utf8 shell 中它可以正常工作并显示“Coordonnées”,但在我家里的 mintty 终端中它显示“CoordonnM-CM-)es”。知道如何让它工作,或者我应该去哪里查看(mintty 没有字符编码选项)吗?

代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys, time, random, copy, pprint
import termios, tty, fcntl
import thread, curses
import locale

def curses_main(stdscr, code):
    curses.curs_set(0)
    stdscr.addstr(2, 0, "Coordonnées")
    stdscr.refresh()
    # (1) = délai d'attente de 1/10 ème de seconde :
    curses.halfdelay(1)
    while True:
        try:
            c=stdscr.getch()
        except:
            c=-1
        if 0<c<256:
            if chr(c) in 'Qq':
                break
def main():
    locale.setlocale(locale.LC_ALL, '')
    code = locale.getpreferredencoding()
    curses.wrapper(curses_main, code)

if __name__ == '__main__':
    main()

更新

错误:我以为找到了: 它无处不在,以该代码开头:

locale.setlocale(locale.LC_ALL, '')

所以我尝试了它并且成功了:

locale.setlocale(locale.LC_ALL, 'fr_FR')

=> 强制为法语

... 它有效,但仅适用于口音。 如果我尝试打印线条图字符:

stdscr.addstr(20,20, "─ │ ┌ ┘ ┐ └ ├ ┤ ┬ ┴ ┼ ═ ║ ╔ ╝ ╗ ╚ ╠ ╣ ╦ ╩ ╬")

给我:

M-b~T~@ M-b~T~B M-b~T~L M-b~T~X M-...

令人头疼的是 vim 正确地 显示了那些字符。所以它似乎来自...来自 python? 从我的 Windows/cygwin/mintty 中运行:

env | grep -i lang
LANG=C.UTF-8

如果我在我的 Windows/cygwin/mintty 中强制编码,那工作:

locale.setlocale(locale.LC_ALL, 'C.UTF-8')

这行不通。

知道我可以去哪里找吗? python 似乎正确地覆盖了语言环境设置...控制台已正确配置,并且字体可以显示线条图字符,因为我用 vim 看到了它们。所以想多了,可能是python的问题,也可能是curses的问题……

最佳答案

您的语言环境首选编码和您的 tty 编码不必相同。

检查 sys.stdin.encoding 和 sys.stdout.encoding 是什么并尝试使用它。

疯狂的猜测是 mintty 的默认编码是 UTF8,但您的语言环境首选编码是(因为它是 Windows)cp1252。

关于python - mintty 终端、python、curses 和 utf8 : incorrect display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4780591/

相关文章:

python - 对 python 正则表达式进行回查

python - 列名称和绘图中的 Groupby 字符串

python - 取出列表中某些项目的最后一个字母

terminal - curses 与 Ctrl+Enter 和其他具有重叠转义序列的击键有何不同?

c - 关闭curses窗口后如何返回正常模式

python - urwid:使光标不可见

python - 如何在spark textFile函数中使用自定义换行符?

python - 为什么来自 python 结果的 shell 脚本中的变量工作奇怪?

python - 诅咒 can_change_color() 总是返回 False

python - nodelay() 导致 python curses 程序退出