escaping - Getting correct string length in Python for strings with ANSI color codes -
I got some Python code that will print a set of automatic data in a column format, with appropriate readability ASCII escake action sequence to paint different pieces of data
I finally put each line in the form of a list, each item is columned with spacepad so that each line always has the same length in the same column. Unfortunately, when I actually print it, all the columns do not line up. I suspect it has to do with the ASCII escape sequences - because len
function does not seem to recognize these:
& gt; & Gt; & Gt; A = '\ x1b [1m0.0 \ x1b [0m' & gt; & Gt; & Gt; Lane (A) 11> gt; & Gt; Print a 0.0
, and whenever every column has the same length according to the lane
, then when printed on the screen, they are not exactly the same length.
There was no way to save some hackery with regular expression (which I did not rather have it) to avoid the string and to figure out what printed length then the space pad I have to do it appropriately? Maybe someone brought it back to "string" and traced its length?
Paipring wiki including matching the ANSI escape sequences:
< code> ESC = literal ( '\ x1B') = integer word (nums) escapeSeq = Combine (ESC + '[' + optional (delimitedList (int, ';')) + oneof (list (alphas)))
how to thus escape-sequence-stripper here:
from pyparsing import * ESC = literal ( '\ x1B') = integer word (nums) escapeSeq = Combine (ESC + '[' '+ optional (delimitedList (integer,'; ')) + oneof (list (alphas)) nonAnsiString = lambda Issues: Pressing (escapeSeq) .transformString (s) unColorString = nonAnsiString ( '\ x1B [1m0.0 \ x1B [0m') print unColorString, Lane (unColorString)
Print:
0.0 3
Comments
Post a Comment