문제

I have below json data

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"}}

How can I use ZIP and put in rows and columns list in web2py ? I tried using

rows = zip(*col[c] for c in columns)

But I am not getting correct output. Columns should be

columns = ["NAME", "ID", "DEPT", "JOIN", "DL", "SNAME", "UPLOAD", "RETRY", "IP"]

rows are associated values in json.

I am looking to have table in below format

NAME, ID, DEPT, JOIN, DL, SNAME, UPLOAD, RETRY, IP
aaa   111 888   666   333 John   600     3     111.222.333.444

....similarly all other values associated with keys.

도움이 되었습니까?

해결책

You mean this perhaps?

columns = col.values()[0].keys()
zip(columns, zip(*(v.values() for v in col.values())))

where col references your dictionary and columns is a list produced from the keys of one of the nested dictionaries.

It is important that you produce columns from a nested dictionary, otherwise you wouldn't get the keys and values in the same order.

Demo:

>>> 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'))]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top