我问过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/