我有以下json数据
col = {"00:00:00:00": {"NAME":"aaa","ID":"111","DEPT":"8888","JOIN":"666","DL":333,"SNAME":"John","UPLOAD":"600","RETRY":"3","IP":"111.222.333.444"},
"11:11:11:11:11": {"NAME":"bbb","ID":"222","DEPT":"9999","JOIN":"777","DL":222,"SNAME":"Mary","UPLOAD":"200","RETRY":"2","IP":"122.133.144.155"},
"22:22:22:22": {"NAME":"ccc","ID":"333","DEPT":"6666","JOIN":"111","DL":333,"SNAME":"Stuart","UPLOAD":"500","RETRY":"4","IP":"199.188.177.166"}}
如何在web2py中使用ZIP并放入行和列列表? 我尝试使用
rows = zip(*col[c] for c in columns)
但我没有得到正确的输出。 列应该是
columns = ["NAME", "ID", "DEPT", "JOIN", "DL", "SNAME", "UPLOAD", "RETRY", "IP"]
行是json中的关联值。
我希望以下面的格式提供表格
NAME, ID, DEPT, JOIN, DL, SNAME, UPLOAD, RETRY, IP
aaa 111 888 666 333 John 600 3 111.222.333.444
....类似于与键相关的所有其他值。
答案 0 :(得分:2)
你的意思是这个吗?
columns = col.values()[0].keys()
zip(columns, zip(*(v.values() for v in col.values())))
其中col
引用您的字典,columns
是根据一个嵌套字典的键生成的列表。
从嵌套字典中生成columns
非常重要,否则您将无法以相同的顺序获取键和值。
演示:
>>> col = {"00:00:00:00":{"NAME":"aaa","ID":"111","DEPT":"8888","JOIN":"666","DL":333,"SNAME":"John","ULOAD":"600","RETRY":"3","IP":"111.222.333.444"},
... "11:11:11:11:11":{"NAME":"bbb","ID":"222","DEPT":"9999","JOIN":"777","DL":222,"SNAME":"Mary","UPLOAD":"200","RETRY":"2","IP":"122.133.144.155"},
... "22:22:22:22":{"NAME":"ccc","ID":"333","DEPT":"6666","JOIN":"111","DL":333,"SNAME":"Stuart","UPLOAD":"500","RETRY":"4","IP":"199.188.177.166"}}
>>> columns = col.values()[0].keys()
>>> zip(columns, zip(*(v.values() for v in col.values())))
[('DEPT', ('9999', '8888', '6666')), ('DL', (222, 333, 333)), ('SNAME', ('Mary', 'John', 'Stuart')), ('JOIN', ('777', '666', '111')), ('NAME', ('bbb', 'aaa', 'ccc')), ('IP', ('122.133.144.155', '600', '199.188.177.166')), ('RETRY', ('2', '111.222.333.444', '4')), ('ID', ('222', '3', '333')), ('UPLOAD', ('200', '111', '500'))]
>>> pprint(zip(columns, zip(*(v.values() for v in col.values()))))
[('DEPT', ('9999', '8888', '6666')),
('DL', (222, 333, 333)),
('SNAME', ('Mary', 'John', 'Stuart')),
('JOIN', ('777', '666', '111')),
('NAME', ('bbb', 'aaa', 'ccc')),
('IP', ('122.133.144.155', '600', '199.188.177.166')),
('RETRY', ('2', '111.222.333.444', '4')),
('ID', ('222', '3', '333')),
('UPLOAD', ('200', '111', '500'))]