twic at urchin.earth.li
Sat Nov 26 21:32:22 CET 2005
Chris, as well as addressing what i think is causing your problem, i'm
going to point out some bits of your code that i think could be polished a
little. It's intended in a spirit of constructive criticism, so i hope you
On Sat, 26 Nov 2005, Chris wrote:
> if item[0:1]=="-":
item[0:1] seems a rather baroque way of writing item! I'd actually
suggest writing this line like this:
As i feel it's more readable.
> item=item[ :-7]
You could just write:
item = item[1:7]
For those two lines.
The "r" isn't necessary - reading is the default mode for files. You could
argue that this documents your intentions towards the file, i suppose, but
the traditional python idiom would leave it out.
> while infile:
The pythonic idiom for this is:
for dummy in infile:
Although i'd strongly suggest you change 'dummy' to a more descriptive
variable name; i use 'line' myself.
Now, this is also the line that i think is at the root of your trouble:
readline returns lines with the line-terminator ('\n' or whatever it is on
your system) still on them. That gets you into trouble later - see below.
When i'm iterating over lines in a file, the first thing i do with the
line is chomp off any trailing newline; the line after the for loop is
line = line.rstrip("\n")
> if dummy=='':break
You don't by any chance mean 'continue' here, do you?
> print item
> print ", "+dummy
> if (dummy == item): <This comparison isn't working>
This is where it all falls down - i suspect that what's happening here is
that dummy has a trailing newline, and item doesn't, so although they look
very similar, they're not the same string, so the comparison comes out
false. Try throwing in that rstrip at the head of the loop and see if it
Gotta treat 'em mean to make 'em scream.
More information about the Python-list