mintty 终端、python、curses 和 utf8:显示不正确
我已询问 这里,但我很确定我不会得到答案。
- 复制 - 粘贴此 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的问题……
I've asked here but I'm pretty sure I won't get an answer.
- copy - paste this python code :
- execute it
- in a classical utf8 shell it work properly and displays "Coordonnées" but in my mintty terminal at home it displays "CoordonnM-CM-)es". Any idea how I could make it work, or where I should look (mintty has no character encoding options)?
code:
#!/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()
Update
Error: I thought found it:
it's written everywhere to begin with that code:
locale.setlocale(locale.LC_ALL, '')
so i tried with that and it worked:
locale.setlocale(locale.LC_ALL, 'fr_FR')
=> force it to French language
...
it worked but only for the accents.
If i try to print line drawing chars:
stdscr.addstr(20,20, "─ │ ┌ ┘ ┐ └ ├ ┤ ┬ ┴ ┼ ═ ║ ╔ ╝ ╗ ╚ ╠ ╣ ╦ ╩ ╬")
gives me:
M-b~T~@ M-b~T~B M-b~T~L M-b~T~X M-...
what is head banging is that vim displays properly those chars. So it seems like it comes... from python?
From my Windows/cygwin/mintty that works:
env | grep -i lang
LANG=C.UTF-8
if I force encoding at home in my Windows/cygwin/mintty that doesn't work:
locale.setlocale(locale.LC_ALL, 'C.UTF-8')
this doesn't work.
Any idea where I could look? python seems to override properly the locale settings... the console is properly configured, and the fonts can display line drawing chars, because I see them with vim. So thinking more about it, it may be either a problem of python or a problem of curses...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的区域设置首选编码和您的 tty 的编码不必相同。
检查 sys.stdin.encoding 和 sys.stdout.encoding 是什么并尝试使用它。
大胆猜测,mintty 的默认编码是 UTF8,但您的语言环境首选编码是(因为它是 Windows)cp1252。
Your locales preferred encoding and your tty's encoding doesn't have to be the same.
Check what sys.stdin.encoding and sys.stdout.encoding is and try to use that.
Wild guesses is that the default encoding for mintty is UTF8, but your locales preferred encoding is (since it's windows) cp1252.
解决了。这就是我所做的:我重新安装了 cygwin,然后在工作中重新打包了我的 python 程序,重新下载了它,然后重新安装了 minnty 0.9.5.1 (mintty-0.9.5-cygwin17.zip),它现在可以工作了。奇怪的。
It's solved. here's what I did: I re-installed cygwin then repacked my python program at work, a re-downloaded it then re-installed minnty 0.9.5.1 (mintty-0.9.5-cygwin17.zip) and it works now. Strange.