I am pulling data from the web and want to align it in a table in a terminal window. I can align the text fine in most cases but when the text contains certain symbols or foreign characters things get messy. How can I handle these characters? Here is an example with the problem on the third line of output:
>>> items = "Apple tree", "Banana plant", "Orange ??", "Goodbye"
>>> values = 100, 200, 300, 400
>>> for i, v in zip(items, values):
... print "%-15s : %-4s" % (i, v)
...
Apple tree : 100
Banana plant : 200
Orange ?? : 300
Goodbye : 400
>>>
Note: I quoted all the items correctly. The "Orange"
closing quotes don't show correctly here on Stack Overflow but they display fine in the terminal window.
UPDATE: I have added a bounty to this question. I am looking for a solution that can be implemented without too much additional code and without using external libraries. It should also work with python 2.7+ and 3.x (conditionals that test for versions and apply different fixes would be fine). Also it should not require any additional system configuration or changing of fonts or changing any terminal settings of a standard Debian/Ubuntu installation.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…