[Tutor] Brain In Vice: Why is this so fun to me?

doug shawhan doug.shawhan at gmail.com
Thu Apr 20 01:20:26 CEST 2006


I think I'm going to have to suck it up and learn some regular expressions.

I have finally gotten my script (using the excellent pyserial module) to
behave. Most of my troubles as enumerated here before were utterly
self-induced. Apparently one cannot watch the execution of one's script
through another program without affecting it's outcome in unforseen ways.
(Sound familiar Herr Schroedinger? :-)

Now that I am actually extracting data in a fairly predictable way, I am at
the point where I need to parse it! I have some output here (modified to
show  the ESC and NUL characters.)

When I pull data from the port, the remote computer sends it in one long
string per screen: newlines are not painted in by using the expected x\0a
that I had hoped for! No easy readlines() fun for me. Instead I get:

ESC=(  1. ESC=($4x2, 6-239 (3.9L)
..........................................ESC=(a03252  ESC=(k0
ESC=)  2. ESC=))8-318 (5.2L)
..........................................ESC=)a03242  ESC=)k0
ESC=*  3. ESC=*)8-360 (5.9L)
..........................................ESC=*a03351  ESC=*k    0
ESC=+  4. ESC=+$4x4, 6-239 (3.9L)
..........................................ESC=+a03240  ESC=+k    0
ESC=,  5. ESC=,)8-318 (5.2L)
..........................................ESC=,a03243  ESC=,k    0
ESC=-  6. ESC=-)8-360 (5.9L)
..........................................ESC=-a03352  ESC=-k    0
ESC=.  7. ESC=.aCH8299  ESCTNULESC)NULESC=% LINEESCTNULESC=&      R =
RELIST  <return> = NONE

I have broken it up for ease of viewing. I need to split the string where
ESC= , k  and 0 are found, but ESC= ,k and 0 are seperated by various
spaces, parentheis and other characters that are apparently used to mark the
end of the line until the next ESC is found, thereby displaying a new line
(note how the character after the first ESC on each line is repeated after
the ESC on the end.

I cannot for the life of me figure out a pythonic way (read: using the
split() builtin) to scan for instances of these characters in such and such
order and proximity. I know this is what regex is for, but I have no
experience.  I have obtained a copy of "Mastering Regular Expressions" but
thought I would inquire here first for caveats and tips as the book is
larger than my brain, and I haven't used the re module, ever.

Why in the world does this make me so happy? :-)~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060419/0ddaec9f/attachment-0001.html 


More information about the Tutor mailing list