windows 在python中绘制方框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/664991/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:10:36  来源:igfitidea点击:

box drawing in python

pythonwindowsunicode

提问by Rook

Platform: WinXP SP2, python 2.5.4.3. (activestate distribution)

平台:WinXP SP2,python 2.5.4.3。(活动状态分布)

Has anyone succeded in writing out box drawing charactersin python? When I try to run this:

有没有人成功地在 python中写出框绘图字符?当我尝试运行它时:

print u'\u2500'
print u'\u2501'
print u'\u2502'
print u'\u2503'
print u'\u2504'

All tips appreciated. What am I doing wrong ? Does python support full unicode ? Is it possible at all to get those characters to print.

所有提示表示赞赏。我究竟做错了什么 ?python 支持完整的 unicode 吗?是否有可能打印这些字符。

Related

有关的

回答by Jiri

Your problem is not in Python but in cmd.exe. It has to be set to support UTF-8. Unfortunately, it is not very easy to switch windows console (cmd.exe) to UTF-8 "Python-compatible" way.

您的问题不在 Python 中,而在 cmd.exe 中。它必须设置为支持 UTF-8。不幸的是,将 Windows 控制台 (cmd.exe) 切换到 UTF-8“Python 兼容”方式并不是一件容易的事。

You can use command (in cmd.exe) to switch to UTF8:

您可以使用命令(在 cmd.exe 中)切换到 UTF8:

chcp 65001

but Python (2.5) does not recognize that encoding. Anyway you have to set correct font that support unicode!

但是 Python (2.5) 无法识别该编码。无论如何,您必须设置支持unicode的正确字体!

For box drawing, I recommend to use old dos codepage 437, so you need to set up it before running python script:

对于方框图,我建议使用旧的 dos 代码页 437,因此您需要在运行 python 脚本之前对其进行设置:

chcp 437

Then you can print cp437 encoded chars directly to stdout or decode chars to unicode and print unicode, try this script:

然后你可以将 cp437 编码的字符直接打印到标准输出或将字符解码为 un​​icode 并打印 unicode,试试这个脚本:

# -*- coding: utf-8 -*- 
for i in range(0xB3, 0xDA):
    print chr(i).decode('cp437'),

# without decoding (see comment by J.F.Sebastian)
print ''.join(map(chr, range(0xb3, 0xda)))

However, you can use box drawing chars, but you cannot use other chars you may need because of limitation of cp437.

但是,您可以使用框绘图字符,但由于 cp437 的限制,您不能使用您可能需要的其他字符。

回答by Ignacio Vazquez-Abrams

This varies greatly based on what your terminal supports. If it uses UTF-8, and if Python can detect it, then it works just fine.

这根据您的终端支持的内容而有很大差异。如果它使用 UTF-8,并且 Python 可以检测到它,那么它就可以正常工作。

>>> print u'\u2500'
─
>>> print u'\u2501'
━
>>> print u'\u2502'
│
>>> print u'\u2503'
┃
>>> print u'\u2504'
┄

回答by Jerub

Printing them will print in the default character encoding, which perhaps is not the right encoding for your terminal.

打印它们将以默认字符编码打印,这可能不是您终端的正确编码。

Have you tried transcoding them to utf-8 first?

您是否尝试先将它们转码为 utf-8?

print u'\u2500'.encode('utf-8')
print u'\u2501'.encode('utf-8')
print u'\u2502'.encode('utf-8')
print u'\u2503'.encode('utf-8')
print u'\u2504'.encode('utf-8')

This works for me on linux in a terminal that supports utf-8 encoded data.

这对我在支持 utf-8 编码数据的终端中的 linux 上有效。

回答by jfs

Python supports Unicode. It is possible to print these characters.

Python 支持 Unicode。可以打印这些字符。

For example, see my answerto "Default encoding for python for stderr?"where I've showed how to print Unicode to sys.stderr(replace it by sys.stdoutfor bare printstatements).

例如,请参阅“stderr 的 Python 的默认编码?”的回答我已经展示了如何将 Unicode 打印到sys.stderr(将其替换sys.stdout为裸print语句)。