I've got some Python code that will automatically print a set of data in a nice column format, including putting in the appropriate ASCII escape sequences to color various pieces of the data for readability.
I eventually end up with each line being represented as a list, with each item being a column that is space-padded so that the same columns on each line are always the same length. Unfortunately when I actually go to print this, not all the columns line up. I suspect this is to do with the ASCII escape sequences - because the len
function doesn't seem to recognize these:
>>> a = '\x1b[1m0.0\x1b[0m'
>>> len(a)
11
>>> print a
0.0
And so while each column is the same length according to len
, they are not actually the same length when printed on the screen.
Is there any way (save for doing some hackery with regular expressions which I'd rather not do) to take the escaped string and find out what the printed length is so I can space pad appropriately? Maybe some way to just "print" it back to string and examine the length of that?
The pyparsing wiki includes this helpful expression for matching on ANSI escape sequences:
Here's how to make this into an escape-sequence-stripper:
prints:
If you're just adding color to some cells, you can add 9 to the expected cell width (5 hidden characters to turn on the color, 4 to turn it off), e.g.
Giving
I don't understand TWO things.
(1) It is your code, under your control. You want to add escape sequences to your data and then strip them out again so that you can calculate the length of your data?? It seems much simpler to calculate the padding before adding the escape sequences. What am I missing?
Let's presume that none of the escape sequences change the cursor position. If they do, the currently accepted answer won't work anyway.
Let's assume that you have the string data for each column (before adding escape sequences) in a list named
string_data
and the pre-determined column widths are in a list namedwidth
. Try something like this:Update-1
After OP's comment:
If the data is built up of pieces each with its own formatting, you can still compute the displayed length and pad as appropriate. Here's a function which does that for one cell's contents:
If you think that the call is overburdened by punctuation, you could do something like:
(2) I don't understand why you don't want to use the supplied-with-Python regular expression kit? No "hackery" (for any possible meaning of "hackery" that I'm aware of) is involved:
Update-2
After OP's comment:
More concise than what? Isn't the following regex solution concise enough for you?
[Above code corrected after @Nick Perkins pointed out that it didn't work]
Looking in ANSI_escape_code, the sequence in your example is Select Graphic Rendition (probably bold).
Try to control column positioning with the CUrsor Position (
CSI n ; m H
) sequence. This way, width of preceding text does not affect current column position and there is no need to worry about string widths.A better option, if you target Unix, is using the curses module window-objects. For example, a string can be positioned on the screen with: