Parse variable into variables >Re: Apology

Joshua Muskovitz joshm at taconic.net
Wed Feb 27 07:06:28 CET 2002


> testfile = open('test','r')
> varset = testfile.readline()
> tple = varset.split()
> vic['AI']['1'], vic['AI']['2'], vic['AI']['3'] = tple
> I assume this will work???

Almost.

vic = {}
testfile = open('test','r')
varset  = testfile.readline()
tple = varset.split()
vic[('AI','1')], vic[('AI','2')], vic[('AI','3')] = tple
testfile.close() # don't forget to clean up after yourself

and yes, you can shorten the entire thing down to:

vic[('AI','1')], vic[('AI','2')], vic[('AI','3')] =
open('test','r').readline().split()

You don't have to close the file in this case, because the file handle goes
out of scope after the line ends, and the file closes automatically.  Of
course, if your file doesn't have exactly three fields separated by white
space on the first line, this will probably throw an exception.
--
# Joshua Muskovitz
# joshm at taconic.net
def lyyrs(sig): return '-'.join(sig.split()+["ly y'rs"])
lyyrs('Hire me!  I need the work!')




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list