string to variable name
Alexander Schmolck
a.schmolck at gmx.net
Thu Apr 24 20:19:38 EDT 2003
Alexander Schmolck <a.schmolck at gmx.net> writes:
> > FILE = open("<mytextfile");
> > while (<FILE>){
> > /^(\w+)\s+(\w+)\s+(\w+)$/;
> > inst.$2 = $1;
> > }
> > }
>
>
> > Is there any way to (easily) achieve what I want to do in python?
>
> Sure: you can do it even shorter and almost as unreadably with (UNTESTED):
>
> for line in file("mytextfile"):
> inst.__dict__.update(dict([line.split()[:2]]))
make that ``split()[1::-1]`` (you have key second, value first, right?)
(this will only work in python2.3, though -- but then setattr is much better
here anyway)
'as
More information about the Python-list
mailing list