在python中绘制方框 [英] box drawing in python

查看:92
本文介绍了在python中绘制方框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

感谢所有提示.我究竟做错了什么 ?python 是否支持完整的 unicode ?是否有可能打印这些字符.

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 中,而在 cmd.exe 中.它必须设置为支持 UTF-8.不幸的是,将 Windows 控制台 (cmd.exe) 切换到 UTF-8Python 兼容"方式并不是很容易.

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.

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

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

chcp 65001

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

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

对于框画,我推荐使用旧的dos codepage 437,所以你需要在运行python脚本之前设置它:

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

chcp 437

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

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

# -*- 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)))

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

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

这篇关于在python中绘制方框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆