On 2014-05-10 02:22, eckhle...@gmail.com wrote:
I'm migrating from Perl to Python and unable to identify the equivalent of key 
of key concept. The following codes run well,

import csv

attr = {}

with open('test.txt','rb') as tsvin:
     tsvin = csv.reader(tsvin, delimiter='\t')

     for row in tsvin:
         ID = row[1]


until:
         attr[ID]['adm3'] = row[2]

I then try:
         attr[ID].adm3 = row[2]

still doesn't work. Some posts suggest using module dict but some do not. I'm a 
bit confused now. Any suggestions?

Python doesn't have Perl's autovivication feature. If you want the
value to be a dict then you need to create that dict first:

attr[ID] = {}
attr[ID]['adm3'] = row[2]

You could also have a look at the 'defaultdict' class in the
'collections' module.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to