我正在尝试用Python解析cassandra cfstats。到目前为止,我取得了一些成功,但我无法按照我想要的格式获取字典。我很亲密,但还没到。
我想要的JSON结构:
{
"data": [{
"{#KP}": "system_traces",
"{#TABLE}": "events"
}, {
"{#KP}": "system_traces",
"{#TABLE}": "sessions"
}, {
"{#KP}": "system",
"{#TABLE}": "IndexInfo"
}]
}
JSON我得到了:
{
"data": [{
"{#KP}": "system_traces"
}, {
"{#TABLE}": "events"
}, {
"{#TABLE}": "sessions"
}, {
"{#KP}": "system"
}, {
"{#TABLE}": "IndexInfo"
}, {
"{#TABLE}": "available_ranges"
}, {
"{#TABLE}": "batches"
}, {
"{#TABLE}": "batchlog"
}, {
"{#TABLE}": "built_views"
}]
}
我到目前为止的代码:
def parse_values(self, text):
text = text.strip().split('\n')
values = []
for line in text:
cfstats = {}
line = line.strip()
if not line or line.startswith('-'):
continue
name, value = line.strip().split(': ', 1)
# append keyspaces and tables to list
if name == "Keyspace":
cfstats['{#KP}'] = value
values.append(cfstats)
elif name == "Table":
cfstats['{#TABLE}'] = value
values.append(cfstats)
return json.dumps({'data': values})
Python 2中是否有加入/级联词典的方法?
答案 0 :(得分:0)
您可以使用pandas进行此类操作。
import pandas as pd
dd = pd.read_json(json_data)['data'].apply(pd.Series)
dd['{#KP}'] = dd['{#KP}'].fillna(method='ffill')
dd[pd.notnull(dd['{#TABLE}'])].to_dict('records')
最后一行的评估结果为:
[{'{#KP}': 'system_traces', '{#TABLE}': 'events'},
{'{#KP}': 'system_traces', '{#TABLE}': 'sessions'},
{'{#KP}': 'system', '{#TABLE}': 'sessions'},
{'{#KP}': 'system', '{#TABLE}': 'IndexInfo'},
{'{#KP}': 'system', '{#TABLE}': 'available_ranges'},
{'{#KP}': 'system', '{#TABLE}': 'batches'},
{'{#KP}': 'system', '{#TABLE}': 'batchlog'},
{'{#KP}': 'system', '{#TABLE}': 'built_views'}]
这是做什么的:
答案 1 :(得分:0)
您可以更改代码,以便只为"Table"
行创建词典,而"Keyspace"
行只更新keyspace
行(在以后的词典中使用{ {1}}行):
"Table"