gpt4 book ai didi

python - 有没有办法将 json 压缩到 web2py 中的行和列中?

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:20 25 4
gpt4 key购买 nike

我有下面的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"}}

如何使用 ZIP 并在 web2py 中放入行和列列表?我尝试使用

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

....类似地,与键关联的所有其他值。

最佳答案

你可能是这个意思?

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

其中 col 引用您的字典,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'))]

关于python - 有没有办法将 json 压缩到 web2py 中的行和列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23091907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com