How can I output colored text to the terminal, in Python? What is the best Unicode symbol to represent a solid block?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
For the characters
Your terminal most probably uses Unicode (typically UTF-8 encoded) characters, so it's only a matter of the appropriate font selection to see your favorite character. Unicode char U+2588, "Full block" is the one I would suggest you use.
Try the following:
Examine the file later with your favourite viewer.
For the colors
curses is the module you want to use. Check this tutorial.
You want to learn about ANSI escape sequences. Here's a brief example:
For more info see http://en.wikipedia.org/wiki/ANSI_escape_code
For a block character, try a unicode character like \u2588:
Putting it all together:
Building on @joeld answer, using https://pypi.python.org/pypi/lazyme
pip install -U lazyme
:Screenshot:
Some updates to the
color_print
with new formatters, e.g.:Note:
italic
,fast blinking
andstrikethrough
may not work on all terminals, doesn't work on Mac / Ubuntu.E.g.
Screenshot:
asciimatics provides a portable support for building text UI and animations:
Asciicast:
For Windows you cannot print to console with colors unless you're using the win32api.
For Linux it's as simple as using print, with the escape sequences outlined here:
Colors
For the character to print like a box, it really depends on what font you are using for the console window. The pound symbol works well, but it depends on the font:
If you are using Django
snapshot:
(I generally use colored output for debugging on runserver terminal so I added it.)
You can test if it is installed in your machine:
$ python -c "import django; print django.VERSION"
To install it check: How to install Django
Give it a Try!!