【发布时间】:2020-02-05 17:17:39
【问题描述】:
关于这个话题的另一个问题。
这个社区有几个关于它的问题和答案:
- how to convert json to csv in python
- Convert Json to CSV using Python
- Python convert JSON to CSV
- Convert JSON to CSV with Python 3
- 接下来...
但我认为在这种情况下,它不适用于这种硬 json:
我的 JSON:
{
"took":32,
"timed_out":false,
"_shards":{
"total":4,
"successful":4,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":94,
"relation":"eq"
},
"max_score":"None",
"hits":[
]
},
"aggregations":{
"hostname":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"396",
"doc_count":47,
"process_name":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":16,
"buckets":[
{
"key":"accounts-daemon",
"doc_count":2,
"process_state":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"sleeping",
"doc_count":2,
"process_pid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1092,
"doc_count":2,
"process_ppid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1,
"doc_count":2,
"process_pgid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1092,
"doc_count":2,
"process_cmdline":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/usr/lib/accountsservice/accounts-daemon",
"doc_count":2,
"process_username":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"root",
"doc_count":2,
"process_cwd":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/",
"doc_count":2,
"process_cpu_start_time":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1570456346000,
"key_as_string":"2019-10-07T13:52:26.000Z",
"doc_count":2,
"process_fd_limit_hard":{
"value":4096.0
},
"process_fd_open":{
"value":8.0
},
"process_memory_size":{
"value":281055232.0
},
"process_memory_rss_bytes":{
"value":6168576.0
},
"process_memory_share":{
"value":5464064.0
},
"process_cpu_total_pct":{
"value":0.0005
},
"process_fd_limit_soft":{
"value":1024.0
},
"process_cpu_system_ticks":{
"value":0.0
},
"process_cpu_user_ticks":{
"value":0.0
},
"process_cpu_total_norm_pct":{
"value":0.0005
},
"process_cpu_total_ticks":{
"value":0.0
},
"process_cpu_total_value":{
"value":18775.0
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
},
{
"key":"961",
"doc_count":47,
"process_name":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":16,
"buckets":[
{
"key":"accounts-daemon",
"doc_count":2,
"process_state":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"sleeping",
"doc_count":2,
"process_pid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1070,
"doc_count":2,
"process_ppid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1,
"doc_count":2,
"process_pgid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1070,
"doc_count":2,
"process_cmdline":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/usr/lib/accountsservice/accounts-daemon",
"doc_count":2,
"process_username":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"root",
"doc_count":2,
"process_cwd":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/",
"doc_count":2,
"process_cpu_start_time":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1570450883000,
"key_as_string":"2019-10-07T12:21:23.000Z",
"doc_count":2,
"process_fd_limit_hard":{
"value":4096.0
},
"process_fd_open":{
"value":8.0
},
"process_memory_size":{
"value":281153536.0
},
"process_memory_rss_bytes":{
"value":5992448.0
},
"process_memory_share":{
"value":5279744.0
},
"process_cpu_total_pct":{
"value":0.0
},
"process_fd_limit_soft":{
"value":1024.0
},
"process_cpu_system_ticks":{
"value":0.0
},
"process_cpu_user_ticks":{
"value":0.0
},
"process_cpu_total_norm_pct":{
"value":0.0
},
"process_cpu_total_ticks":{
"value":0.0
},
"process_cpu_total_value":{
"value":13410.0
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
我几乎尝试了所有方法,并使用可怕的多循环代码解析它,但我认为有更简单的方法可以做到这一点,但这个社区中没有任何帖子可以帮助我。
你能帮我得到这样的东西吗?:
我的结果(不完整):
hostname process_name process_state ... process_username process_cwd process_cpu_start_time
0 396 accounts-daemon sleeping ... root / 2019-10-07T13:52:26.000Z
1 396 iscsid sleeping ... root / 2019-10-07T13:52:27.000Z
2 396 iscsid sleeping ... root / 2019-10-07T13:52:28.000Z
3 396 ksoftirqd/0 sleeping ... root / 2019-10-07T13:52:28.000Z
4 396 kworker/0:1 sleeping ... root / 2019-10-07T13:52:28.000Z
.. ... ... ... ... ... ... ...
25 961 polkitd sleeping ... root / 2019-10-07T12:21:31.000Z
26 961 rcu_sched sleeping ... root / 2019-10-07T12:21:31.000Z
27 961 systemd sleeping ... root / 2019-10-07T12:20:24.000Z
28 961 systemd-journal sleeping ... root / 2019-10-07T12:20:38.000Z
29 961 unattended-upgr sleeping ... root / 2019-10-07T12:21:26.000Z
我知道这并不容易,所以我将不胜感激。
提前致谢。
【问题讨论】:
标签: python json pandas dataframe