Reputation: 62538
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.
Upvotes: 7
Views: 12917
Reputation: 1
I use the Windows Character Map tool and both in CMD and PowerShell it appears fine. Font: Arial. Python code: Exe: Print('■■■■■■■□□□□□□□') Print('●●●●●●○○○○○○○○') And much more!
Upvotes: 0
Reputation: 16625
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.
Upvotes: 6
Reputation: 414315
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).
Upvotes: 0
Reputation: 798716
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'
┄
Upvotes: 2
Reputation: 42638
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.
Upvotes: 1