newbie question about self and eval
Prateep Siamwalla
teep at danaicorp.com
Tue Jan 25 02:38:12 EST 2000
Hello o' wise pythoners,
I have a simple question to ask any generous pythoners out there:
I have a simple tab delim text file:
test.txt
---------
fee harry
fi joe
fo jack
fum jim
fee john
fo sam
I trying to make a class to read in these values and stuff them into lists
which this class keeps
(warning, spacing migth be off)
-------
class myclass:
def __init__(self,filename='c:\\test.txt'):
self.fee = []
self.fi = []
self.fo = []
self.fum = []
f = open(filename,'r')
rl = f.readlines()
f.close()
for lines in rl:
token = string.split(lines,'\011')
# do something more efficient than this !!!
bucket = token[0]
if bucket =='fee':
self.fee.append(token[1])
elif bucket == 'fi':
self.fi.append(token[1])
elif bucket == 'fo':
self.fo.append(token[1])
else:
self.fum.append(token[1])
---------
I know there must be a more effective way of appending items into the
various class lists, (which probably involves eval or something similar, but
I've tried some feeble ideas, none of which seem to fly.
I've tried self.eval(token[0]).append(token[1]) but that obviously failed
in a big way. Could someone please show me the way?
Thanks in Advance,
teep
More information about the Python-list
mailing list