7

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

Community
  • 1
  • 1
Rook
  • 60,248
  • 49
  • 165
  • 242
  • This works for me just fine, do you know if the windows terminal support unicode? – cobbal Mar 20 '09 at 05:04
  • @cobbal - To be frank, I'm not sure. The last time I used those were in the good ol' dos days. Now I'm experimenting, but a lot changed. – Rook Mar 20 '09 at 05:09

5 Answers5

6

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
  • 16,425
  • 6
  • 52
  • 68
  • `print ''.join(map(chr, range(0xb3, 0xda)))` What is the point in decoding to Unicode before printing (when you already done `chcp 437`)? – jfs Mar 20 '09 at 09:10
  • You are right, decoding is not nescessary. In general, I prefer to use unicode everywhere and let output functions handle all the encoding. – Jiri Mar 20 '09 at 09:46
2

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
  • 776,304
  • 153
  • 1,341
  • 1,358
1

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
  • 41,746
  • 15
  • 73
  • 90
0

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

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

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!

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '23 at 16:27