assign dict by value to list

Phill Atwood fakeaddress at nowhere.org
Tue Sep 6 22:36:58 EDT 2005


Newbie question:

I have a dictionary called "rec".  And a list called "db". db is my 
database. rec are to be records in the database. In a loop I'm trying to 
create a record and add it to my database.  The key stmt is

db.append( rec )

This works initially but it seems that instead of copying the values in 
rec and adding it to db, only a reference of rec is added to db.  Thus
when I reassign rec with new data and then do another append I end up
with two records in my database but now the both contain the same data, 
namely the latest version or rec.

So how do I add a dictionary into a list by value rather than by reference?

The complete code is here:

=========================================================
#!/usr/bin/python

import sys

inputfile = sys.argv[1]

keywords = [ 'place-name=',
'addr-line1=',
'addr-line2=',
'addr-city=',
'addr-state=',
'addr-country=',
'addr-postalcode=',
'mailaddr-line1=',
'mailaddr-line2=',
'mailaddr-city=',
'mailaddr-state=',
'mailaddr-country=',
'mailaddr-postalcode=',
'place-phone1=',
'place-phone2=',
'place-fax=',
'place-email1=',
'place-email2=' ]

def csv_it():
     db = []  # the entire database of records
     rec = {} # a single rec: a dictionary of field names and data pairs
     fields = [] # list of field names collected so far for current record
     for line in open(inputfile):
         kword = getkeyword(line) # get keyword (or field name)
         if kword:
             # We've got a line with a keyword in it
             if kword == keywords[0]:
                 # Starting a new record, so save current record
                 db.append( rec )
                 printdb(db, "stage 1")

                 # Now clear current record
                 fields = []
                 rec.clear()
                 dummylist = []
                 for i in range(1,len(keywords)+1):
                     dummylist.append('')
                 for k,d in zip(keywords,dummylist):
                     rec[k] = d
                 printdb(db, "stage 2")

             # make sure we are not encountering a duplicate key word
             if kword in fields:
                 print "Record contains duplicate field"
                 print line,
                 sys.exit()
             fields.append(kword)
             # collect our data and store it in the current record
             data = line[line.find('=')+1:]
             datalstrip = data.lstrip()
             datarstrip = datalstrip.rstrip()
             rec[kword] = datarstrip

     printdb(db,"stage 3")
     db.append( rec ) # don't forget whatever we have in our last record

     # dump the database in comma separated value form
     outstring = ''
     for k in keywords:
         outstring += '"'
         outstring += k
         outstring += '"'
         outstring += ','
     print outstring
     for r in db:
         outstring = ''
         for k in keywords:
             if r[k]:
                 outstring += '"'
                 outstring += r[k]
                 outstring += '"'
             outstring += ','
         print outstring


def getkeyword(line):
     equal_index = line.find('=')
     if equal_index >= 0:
         kword = line[0:equal_index+1]
         if kword in keywords:
             return kword
         else:
             print "Invalid keyword at line:"
             print line
             sys.exit()
     return None

def printdb( db, text ):
     print " "
     print "db %s = " % text
     print db
     print " "



if __name__ == '__main__':
     csv_it()

====================================================

Thanks
Phill



More information about the Python-list mailing list