semi-Newbie question

danielx danielwong at berkeley.edu
Thu Aug 10 16:29:34 EDT 2006


len wrote:
> Hi all
>
> I have a file that I receive from another party which is basicly a csv
> file containing the following type of information;

Python has a csv module. I'm not sure if you're already using that or
if it would be useful to you:

http://docs.python.org/lib/module-csv.html

>
> Tagname             Scope          Value
> "first_name","POL01","John"
> "last_name","POL01","Doe"
> "birthday","POL01","04/03/61"
> etc
>
> I need to convert this file info into my file format used in my
> application.
>
> I have been given a file from the other company that gives me all of
> the tagname that could be used and their scope.  I want to build a

So, a tag name is like a field name, right? What's a scope?

> table which would have all of the various tagnames, scope, and put a
> fieldname equivalent in the fieldname in my file structure in my
> application.  Then read through the vendors csv file index into my
> table file and get the field name of where to move the data into my
> data structure.
>
> Here is my question?  basicly I need to get the data referenced by
> fieldname variable in my table and then use that data as a variable
> name in a python assignment statement. Thus changing the actual python
> code at each iteration through the lines in the csv file.

I'm not sure that you need to have a different statement execute during
each loop. Why not have something like this:

for line in tagfile:
  foreignTag, scope, value = parse(line)
  currentRow[ myEqvTag(foreignTag) ] = value
  ## do you want to do something with the scope information?

If you really wanted to, you could use exec/eval and construct a string
you want to execute/evaluate, but people tend to look down on that. At
least one of the reasons is that it would be slower. Just for
completeness, maybe this is what you want:

for line in tagfile:
  ft, sc, v = parse(line)
  exec "%s = %s" % ( myEqvTag( ft ), v )

Is this something like what you wanted? I'm not quite sure what you're
asking... More code might help.

Good Luck!

>
> for i in tagfile
>   find tagname in tagtable
>
> *** the following line of code would change through each iteration ***
>   myfirst = value
>
> *** next iteration
>   mylast = value
>
> *** next iteration
>   mybirth = value
>
> etc
>
> I hope this make sense.  I remember seeing something like this
> somewhere.
> 
> Any help appreciated.
> 
> Len Sumnler
> Unique Insurance




More information about the Python-list mailing list