Monday, 15 July 2013

Converting nested python lists to database -



Converting nested python lists to database -

i have python list has construction this:

apts = [ [2083, \ [ ["price", "$1000 / month"], \ ["sq ft.", "500"], \ ["amenities", "gym hardwood floor"]]], \ [1096, \ [ ["price", "$1200 / month"], \ ["sq ft.", "700"], \ ["a/c", "true"]]], \ [76, \ [ ["price", "$1100 / month"], \ ["pets", "true"], \ ["a/c", "true"]]]]

how in format such can transfer mysql database? basically, want rearrange in such way resembles table/csv file transferable, like:

id, price, sq ft, amenities, a/c, pets 2083, $1000 / month, 500, gym hardwood floor, , 1096, $1200 / month, 700, , true, 76, $1100 / month, , true, true

thanks in advance. can think of ways in map these piece piece, it's seems pretty inefficient, , knowledge of python weak, i'm hoping there other quick methods converting data...

would helpful if instead of nested list used nested dictionary structure?

my understanding difficulty in converting complex construction strings of values. here how done:

from collections import ordereddict out = [] r in apts: row = ordereddict([('id',''), ('price',''), ('sqft',''), ('amenities',''),('ac',''),('pets','')]) row['id']=r[0] sr in r[1]: row[sr[0].lower().translate(none," ./")]=sr[1] out.append(row) #print result o in out: s = ",".join(map(str, o.values())) print s

prints

2083,$1000 / month,500,gym hardwood floor,, 1096,$1200 / month,700,,true, 76,$1100 / month,,,true,true

python database list dictionary nested

No comments:

Post a Comment