简体   繁体   中英

Print non-ASCII characters

I would like to render the special non-ASCII characters on the console, like the ones used in old DOS programs to draw windows in a terminal. I specifically want to draw the characters they use in rendering windows/dialogs/tables, as shown here:

Norton Commander DOS 菜单

You can use Python curses library, it's a part of standard library on *nix systems, or you can use Urwid that is a higher level library for creating console interfaces.

Constants paragraph of the documentation contains information about how special characters can be drawn in X Emulators (if they support VT100s inherited features). If the emulator doesn't support such alternative character set - ascii approximation is used. I suppose that this is what you're looking for.

Here's how to directly print to a console, although a library like curses is definitely easier.

Figure out what characters your console supports:

import sys
print('encoding =',sys.stdout.encoding)
print(bytes(range(256)).decode(sys.stdout.encoding)
 encoding = cp437 ☺☻♥♦ ♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂Çüéâäà åçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ² ■

Then print them as Unicode characters. Make sure to save the source file in UTF-8 (Python 3's default) or declare the encoding used in the source file with a encoding comment. encoding注释声明源文件中使用的 encodingUTF-8 can handle any character, but if you print one your terminal doesn't support you'll get a UnicodeEncodeError in pre-3.6 Python.

print('╔═╦═╗╓─╥─╖╒═╤═╕┌─┬─┐')
print('║ ║ ║║ ║ ║│ │ ││ │ │')
print('╠═╬═╣╟─╫─╢╞═╪═╡├─┼─┤')
print('║ ║ ║║ ║ ║│ │ ││ │ │')
print('╚═╩═╝╙─╨─╜╘═╧═╛└─┴─┘')
 ╔═╦═╗╓─╥─╖╒═╤═╕┌─┬─┐ ║ ║ ║║ ║ ║│ │ ││ │ │ ╠═╬═╣╟─╫─╢╞═╪═╡├─┼─┤ ║ ║ ║║ ║ ║│ │ ││ │ │ ╚═╩═╝╙─╨─╜╘═╧═╛└─┴─┘

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM