Help: From 2D table to dictionary
pekka niiranen
krissepu at vip.fi
Mon Jan 21 02:56:13 EST 2002
How to convert 2d table to a dictionary as follows:
table is 'FILE' 'co1' 'co2' 'co3'
'row1' 'a' 'b' 'c'
'row2' 'A' 'B' 'C'
or in python syntax:
table = [['FILE', 'co1', 'co2', 'co3'], ['row1', 'a', 'b', 'c'],
['row2', 'A', 'B', 'C']]
How to convert it to a nested dictionary like this:
d = {'row1' : {'co1': 'a', 'co2': 'b', 'co3': 'c'}, 'row2' : {'co1':
'A', 'co2': 'B', 'co3': 'C'}}
I have got this far but forming the dictionary proved more difficult
than I expected:
table
[['FILE', 'co1', 'co2', 'co3'], ['row1', 'a', 'b', 'c'], ['row2', 'A',
'B', 'C']]
for row in table[1:]:
... for col in table[0][1:]:
... x = table.index(row)
... y = table[0].index(col)
... print x, y, table[x][y]
...
1 1 a
1 2 b
1 3 c
2 1 A
2 2 B
2 3 C
Solution would be useful when reading an Excel -table thru
win32-extensions into a dictionary.
-pekka-
More information about the Python-list
mailing list