Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Extended ASCII characters in Python

I'd like to create menu boxes in Python with the following ascii characteres:

ASCII code 200 = ╚ ( Box drawing character double line lower left corner )

ASCII code 201 = ╔ ( Box drawing character double line upper left corner )

ASCII code 202 = ╩ ( Box drawing character double line horizontal and up )

ASCII code 203 = ╦ ( Box drawing character double line horizontal down )

ASCII code 204 = ╠ ( Box drawing character double line vertical and right )

ASCII code 205 = ═ ( Box drawing character double horizontal line )

ASCII code 206 = ╬ ( Box drawing character double line horizontal vertical )

However it seems that these are not supported. When I use chr() the system prints something totally different.

Do you have any advise on how to print the mentioned characters using Python 3?

Thank you.

like image 666
olg32 Avatar asked Sep 05 '17 22:09

olg32


People also ask

How do I display extended ASCII characters?

ASCII Extended Character Set, Windows In order to use this shortcut, hold down the "ALT" key while you type in the entire code. When you release the ALT key the glyph will appear.

How do I print an ASCII character in Python?

Here are few methods in different programming languages to print ASCII value of a given character : Python code using ord function : ord() : It converts the given string of length one, returns an integer representing the Unicode code point of the character. For example, ord('a') returns the integer 97.

Does Python support extended ASCII?

There is no such thing as "extended ASCII", there are many different encodings in which 247 can mean different things. You need to decode the string with the right encoding. The clarification that the most common extension is Latin-1 is very helpful.


2 Answers

It seems to work with string literals:

>>> symbs = [u'\u255a', u'\u2554', u'\u2569', u'\u2566', u'\u2560', u'\u2550', u'\u256c']
>>> for sym in symbs:
...     print(sym)
... 
╚
╔
╩
╦
╠
═
╬

This appears to work on all platforms I've tried it on, Windows 7, Mac OS , Linux, etc.

Codes gotten from http://svn.python.org/projects/stackless/trunk/Lib/encodings/cp720.py

Hope this helps.

like image 151
chickity china chinese chicken Avatar answered Sep 18 '22 07:09

chickity china chinese chicken


Follow-up thanks to Dave Edwards ...

class TableBorder:
    def __init__ (self, top_left, top_split, top_right,
        mid_left, mid_split, mid_right,
        low_left, low_split, low_right,
        horizontal, vertical):
        self.top_left = top_left
        self.top_split = top_split
        self.top_right = top_right
        self.mid_left = mid_left
        self.mid_split = mid_split
        self.mid_right = mid_right
        self.low_left = low_left
        self.low_split = low_split
        self.low_right = low_right
        self.horizontal = horizontal
        self.vertical = vertical

Borders0 = TableBorder ('+', '+', '+', '+', '+', '+', '+', '+', '+', '-', '|')
Borders1 = TableBorder (u'\u250c',u'\u252C',u'\u2510',u'\u251C',u'\u253C',u'\u2524',u'\u2514',u'\u2534',u'\u2518',u'\u2500',u'\u2502')
Borders2 = TableBorder (u'\u2554',u'\u2566',u'\u2557',u'\u2560',u'\u256C',u'\u2563',u'\u255a',u'\u2569',u'\u255d',u'\u2550',u'\u2551')

def draw_box (width, height, box):
    span = width-2
    line = box.horizontal * (span)
    print (box.top_left + line + box.top_right)
    body = box.vertical + (' '*span) + box.vertical
    for _ in range (height-1):
        print (body)
    print (box.low_left + line + box.low_right)

draw_box (20, 10, Borders1)
like image 28
Mikal B Keenan Avatar answered Sep 18 '22 07:09

Mikal B Keenan