[Tutor] best way to get external data

Bryan Fodness bryan.fodness at gmail.com
Fri May 2 17:12:12 CEST 2008


I am trying to figure out the best way to get external data.

Using the following data in a file

     1
     2
     3

I have used,

     fi = open(infile, 'r')
     s = fi.readlines()
     fi.close()
     a = s[0]
     b = s[1]
     c = s[2]

but, if I have,

     x = 1
     y = 2
     z = 3

I have used,

     for line in open(infile):
          i_line = line.split()
          if i_line[0] == 'x':
               a = i_line[2]
           elif i_line[0] == 'y':
               b = i_line[2]
           elif i_line[0] == 'z':
               c = i_line[2]

I do have control over the creation of the data file.  The second way is
better in my mind, because it is independent of the ordering.

If I have a long list of 'variable' = 'value', could this be better managed
in a dictionary?  Or, is there a better way?

Thanks


-- 
"The game of science can accurately be described as a never-ending insult to
human intelligence." - João Magueijo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080502/ef6d1670/attachment.htm>


More information about the Tutor mailing list