[Tutor] character that means beginning of line for readlines purposes

Vicki Stanfield vicki at stanfield.net
Mon Mar 22 09:48:21 EST 2004


I am parsing data from an input file. Sometimes the data must be converted
and other times not. If for instance, the 0x11| comes at the beginning of
a line, it gets converted and if it comes in the middle of the data line,
it doesn't. I know how to do this with search in the vi editor; "^" means
beginning of line. I found information that the same RE is used in Python.
For some reason, I can't get it to work the way I want. I am using split
to separate the line into tokens if that matters. Here is the code that
isn't working:

            if tokens[i] == '0x11':
                if tokens[i-1]in ('0x3', '0x4','^'):
                    new = "0x02|"
                else:
                    new = "0x31|0x31|"
                outputfile.write(new)

The first 0x11, is converted to "0x31|0x31| even though it begins a line.
Can someone explain this? I know that I can toggle a flag to determine
this, but I'd rather do it with a regular expression.

--vicki

"A pessimist sees the difficulty in every opportunity; an optimist sees
the opportunity in every difficulty."
  --  Winston Churchill




More information about the Tutor mailing list