[Tutor] Reading Input Data

lechtlr lechtlr at yahoo.com
Wed Jan 16 16:36:37 CET 2008


  Thank you all for your suggestions.
    The purpose of this script to read values for initialization of a class that calls functions from another software program for chemically reacting flows (www.cantera.org). I have around 25 input variables with distinct variable names (don’t follow any patterns) with 5 strings (i.e., last five variables that contain chemical species names). Currently, I use a csv file to provide the initial values.  I hope this answers your questions.
  Lex

Michael Langford <mlangford.cs03 at gtalumni.org> wrote: I'd like to be clear, this isn't a clean thing for the middle of a big
program. I was thinking the entire time I was testing it "I wonder why
anyone would need to do this...."

But if you have a python program you'd probably call a script, used
for one simple task, it can be appropriate (with Kent's catch on the
globals/local thing). I assumed you were using your approach for
something like that. (would you tell us what you're doing this for
btw? The suspense ... :o))

If you're doing something like multivariable analysis or something
else that you would do in software like maple, this approach can
greatly simplify the notation over the list/dict approach.

If you're not using python as a huge substitute for a math solver,
then avoid what I said like the plague and use a dict.

          --Michael

On 1/15/08, Michael Langford  wrote:
> Accidentally cut off a 0 there...
> Think about using ConfigParser instead of your csv. Doug Hellman wrote
> a good article on that:
> http://blog.doughellmann.com/2007/04/pymotw-configparser.html
>
> But if you really want to load your data this way, this will work:
>
> for subscript,line in enumerate(file("file.csv")):
>      s = line.split(",")[1]
>      try:
>              f = float(s)
>              locals()["x%i" % subscript]=f
>      except:
>              locals()["x%i" % subscript]=s
>
> print x1
> print x0
>
> On Jan 15, 2008 3:47 PM, Michael Langford  wrote:
> > for subscript,line in enumerate(file("file.csv")):
> >      s = line.split(",")[1]
> >      try:
> >              f = float(s)
> >              locals()["x%i" % subscript]=f
> >      except:
> >              locals()["x%i" % subscript]=s
> >
> > print x1
> > print x
> >
> > On Jan 15, 2008 3:26 PM, lechtlr  wrote:
> >
> > > I want to read an input file (file.csv) that has two columns. I want to read
> > > 2nd column and assign variables that are strings and floats. Currently, I
> > > use the following split() function to read from the input file and create a
> > > list, and then assign each element to a variable.
> > >
> > > I am wondering there is any other easier (and elegant) way of doing this ?
> > >
> > > data = []
> > > for line in open("file.csv"):
> > >      columns = line.split(',')
> > >      data.append([columns[1]])
> > >
> > > This script returns, say:
> > > data = [ ['20.0'], ['0.34'], ................,[ 'a:0.20, b:0.30, c:0.50' ]]
> > >
> > > Then, I assign to a set of variables, say:
> > >
> > > x1 = float(data[0][0]) ; x2 = float(data[1][0]);.............;xn =
> > > data[-1][0]
> > >
> > >
> > > Thanks,
> > > Lex
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >  ________________________________
> > > Never miss a thing. Make Yahoo your homepage.
> >
> > > _______________________________________________
> > > Tutor maillist  -  Tutor at python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> > >
> >
> >
> >
> > --
> > Michael Langford
> > Phone: 404-386-0495
> > Consulting: http://www.RowdyLabs.com
> >
>
>
>
> --
> Michael Langford
> Phone: 404-386-0495
> Consulting: http://www.RowdyLabs.com
>


-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com


       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080116/199fa384/attachment.htm 


More information about the Tutor mailing list