[Tutor] best way to get external data

rui ruivaldo at gmail.com
Sat May 3 00:33:01 CEST 2008


PyYaml is an option too. [1]

[1] - http://pyyaml.org/

It´s very readable and is converted to a Python native structure/object.

On Fri, May 2, 2008 at 12:12 PM, Bryan Fodness <bryan.fodness at gmail.com>
wrote:

> 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
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Meu nome é Valdo, Ruivaldo Neto.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080502/66466b8a/attachment-0001.htm>


More information about the Tutor mailing list