views:

321

answers:

4

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

+2  A: 

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?

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.

Jerub
+3  A: 

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'
┄
Ignacio Vazquez-Abrams
+4  A: 

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.

Jiri
`print ''.join(map(chr, range(0xb3, 0xda)))` What is the point in decoding to Unicode before printing (when you already done `chcp 437`)?
J.F. Sebastian
You are right, decoding is not nescessary. In general, I prefer to use unicode everywhere and let output functions handle all the encoding.
Jiri
A: 

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

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

J.F. Sebastian