'nestedxml fetched converted to nested dict. would like advice how to do it efficiently
I'm parsing nested xml from fetched from postgres and packing it into nested dict. In the process I'm creating multiple temp variable to pass value after condition is met. I'm also using nested loop. There is a lot of rows that needs to be fetched from postgres. This is the solution that I came up with but would like to know if this could done is efficient way without complicating. I can't copy paste the code so I rewrote this on here but this seems to work even if there's typing mistakes. Looking for advice to better this code.
innerscore, innerpop = {},{}
stmt = "....."
with conn.cursor(name="segcurs", cursor_factory=psycopg2.extras.RealDictCursor,scrollable=False, withhold=False) as segcurs:
segcurs.intersize=1000
segcurs.execute(stmt)
for row in segcurs:
tree = ET.formstring(row['per'])
for child in tree:
if child.tag ="score":
temp_d = innerscore
if child.tag = "pop":
temp_d = innerpop
for child1 in child:
name = child1.tag
value = child1.text
if not name in temp_d:
temp_d[name] = []
temp_d[name].append(value)
else:
temp_d[name].append(value)
perf={}
perfs['score'] = innerscore
perfs['population'] = innerpop
row = <"per"><"score"><"s1">1</"s1"><"s2>2</"s2"></"score">,<"pop"><"p1">1</"p1"><"p2>2</"p2"></"pop"></"per"> (long xml)
perfs={"score":{"s1":[],"s2":[]}, "population":{"pop1":[], "pop2":[]}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
