python - convert list to nested dic and assign a value -


i trying convert csv json. if encounter csv headers naming convention "columnname1.0.columnname2.0.columnname3" need create nested json --> {columnname1 : {columnname2 : {columnname3 : value }}}..

so far able split header list of subcolumnnames , create nested json type, unable assign value. help?

    data = open(str(filename.strip("'")),'rb')     reader = csv.dictreader(data,delimiter = ',',quotechar='"')      '''          header '''      line in reader:         x,y in line.items():             columns = re.split("\.\d\.",x)             if len(columns) == 1:                 continue             else:                 print "columns %s"%columns                 testline = {}                 subcolumnname in reversed(columns):                     testline = {subcolumnname: testline}                 ''' need assign value y? '''                     print "line%s"%testline 

output:

columns ['experience', 'title'] line{'experience': {'title': {}}} columns ['experience', 'organization', 'profile_url'] line{'experience': {'organization': {'profile_url': {}}}} columns ['experience', 'start'] line{'experience': {'start': {}}} columns ['raw_experience', 'organization', 'profile_url'] line{'raw_experience': {'organization': {'profile_url': {}}}} columns ['raw_experience', 'end'] line{'raw_experience': {'end': {}}} columns ['experience', 'organization', 'name'] line{'experience': {'organization': {'name': {}}}} 

the value want {}, initial value of testline. can try this:

testline = value subcolumnname in reversed(columns):     testline = {subcolumnname: testline} 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -