semi-Newbie question

davideugenewarren at gmail.com davideugenewarren at gmail.com
Thu Aug 10 16:15:06 EDT 2006


Do you absolutely need to use variables?  A dictionary would serve if
each case has a unique identifier.

client_info_dict = {}

for i in tagfile:
     tagname,scope,value = i.replace('"','').split(',') # split fields,
strip redundant characters
     client_info_dict.setdefault(scope,{}) # set up an empty nested
dict for the unique ID
     client_info_dict[scope][tagname] = value # set the tagname's value

Then client info can be retrieved from the dictionary using unique IDs
and stereotyped tags (with .get() if some tags are not always present).
 I won't make any claims about the efficiency of this approach, but it
works for me.

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;
>
> 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
> 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.
>
> 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