Parsing
Duncan Booth
duncan at NOSPAMrcp.co.uk
Thu Apr 17 07:14:07 EDT 2003
iamshady at rediffmail.com (Jim Shady) wrote in
news:9afd3f36.0304170247.2a082573 at posting.google.com:
> What I'd like to do is run through each entry with x.readlines() and
> put all 4 elements in an array for further processing. Right now I'm
> trying with:
>
> for chunk in psfile.readlines():
> if i <= 4:
> cur_chunk.append(chunk[:-2])
> i = i+1
> if i > 4:
> i = 0
> cur_chunk = []
>
> for x in bigchunk:
> print x
>
> This somehow doesn't work. I am a newbie to Python and would
> appreciate any help.
What is the relationship between cur_chunk and bigchunk supposed to be? The
code you posted doesn't do anything with cur_chunk, perhaps you want to
append it to bigchunk before clearing it.
Other hints:
Forget using readlines, just iterate over the file directly.
Dont try to strip newlines from the end of the line using chunk[:-2], much
better just to use the strip method (or the rstrip method if you don't want
to lose leading spaces).
>From the description, it sounds as though the blank lines are separating
the chunks, so you might want to collect lines up to but not including the
blank line.
Putting that altogether you might get something like this:
bigchunk = [[]]
for line in psfile:
line = line.rstrip()
if line:
bigchunk[-1].append(line)
else:
bigchunk.append([])
# Remove last chunk if it wasn't used.
if not bigchunk[-1]:
bigchunk.pop(-1)
import pprint
pprint.pprint(bigchunk)
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list