Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

box drawing in python

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

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'

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

Related

  • Default encoding for python for stderr?
like image 808
Rook Avatar asked Mar 20 '09 04:03

Rook


2 Answers

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.

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

chcp 65001

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

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

chcp 437

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

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

like image 189
Jiri Avatar answered Oct 07 '22 20:10

Jiri


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.

>>> print u'\u2500'
─
>>> print u'\u2501'
━
>>> print u'\u2502'
│
>>> print u'\u2503'
┃
>>> print u'\u2504'
┄
like image 28
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 21:10

Ignacio Vazquez-Abrams