Python prob
Gerhard Häring
gh at ghaering.de
Sat May 31 08:54:31 EDT 2003
Darren Teo wrote:
> Thanks Gerhard
N. p. But let's keep this on-list.
> Another quick question if i were to read from a file and split a
> line with a ":" delimeter eg[why:2:crap:hehe] and output to ['why',
> '2', 'crap', 'hehe'] without the "\n" then I couldn't use the same
> way u showed me ?? Could you tell me where i went wrong ?? Thanks.
>
> intabfile = open(filename, "r")
> m = []
> for tablines in intabfile:
> tab = string.split(tablines, ":" ,10) # (1)
> if tablines:
> m.append(tablines.rstrip())
> intabfile.close()
> return m
The line I marked with "(1)" is a no-operation. To get rid of the
trailing newline, you can use rstrip() method of strings. To split on
the colon, you can use the split method of strings. For some reason you
used the deprecated string module instead.
I seriously think you should try a little harder before on the list. I'm
happy to answer *specific* questions, but I don't plan to do your
homework for you.
Mind you, if your prof is clever, (s)he'll monitor the various obvious
lists and newsgroups, anyway.
-- Gerhard
More information about the Python-list
mailing list