我正在运行Bottle和python来从MongoDB中提取数据。我的输出是JSON对象格式 我曾经有以下
{u'product': u'mortgage', u'Total_Product': 146533}
{u'product': u'debt collection', u'Total_Product': 65639}
但想要摆脱'u并获得以下格式:
'product':'mortgage', Total_Product: 146533
'product':'debt collection' 'Total_Product:65639
运行此代码后,我留下一个没有结果且没有错误消息的空白屏幕。有什么建议吗?
# find a single with all aggregates
choices = dict(
aggr1 = db.complaints.aggregate([{"$group":{"_id":"$disputed", "Total_Disputed":{"$sum":1}}},{"$sort":{"Total_Disputed":-1}},{"$project":{"_id":0, "disputed":"$_id", "Total_Disputed":1}}]),
aggr2 = db.complaints.aggregate([{"$group":{"_id":"$product", "Total_Product":{"$sum":1}}},{"$sort":{"Total_Product":-1}},{"$project":{"_id":0, "product":"$_id", "Total_Product":1}}]),
aggr3 = db.complaints.aggregate([{"$group":{"_id":"$response", "Total_Response":{"$sum":1}}},{"$sort":{"Total_Response":-1}},{"$project":{"_id":0, "response":"$_id", "Total_Response":1}}]),
aggr4 = db.complaints.aggregate([{"$group":{"_id":"$submitted", "Total_Submitted":{"$sum":1}}},{"$sort":{"Total_Submitted":-1}},{"$project":{"_id":0, "submitted":"$_id", "Total_Submitted":1}}]),
aggr5 = db.complaints.aggregate([{"$group":{"_id":"$timely", "Total_Timely":{"$sum":1}}},{"$sort":{"Total_Timely":-1}},{"$project":{"_id":0, "Timely":"$_id", "Total_Timely":1}}])
)
def convert_keys_to_string(choices):
"""Recursively converts dictionary keys to strings."""
if not isinstance(choices, dict):
return choices
return dict((str(k), convert_keys_to_string(v))
for k, v in dictionary.items())
aggr1 = 'aggr1'
aggr2 = 'aggr2'
aggr3 = 'aggr3'
aggr4 = 'aggr4'
aggr5 = 'aggr5'
return bottle.template('analytics.tpl', things1 = choices[aggr1], things2 = choices[aggr2], things3 = choices[aggr3], things4 = choices[aggr4], things5 = choices[aggr5])
bottle.debug(True)
bottle.run(host='localhost', port=8082)
tpl如下:
<!DOCTYPE html>
<html>
<head>
<title> @2015 </title>
</head>
<body>
<p>
</p>
<p>
<ul>
%for thing1 in things1:
<li>{{thing1}}</li>
%end
</ul></p>
<p>
<ul>
%for thing2 in things2:
<li>{{thing2}}</li>
%end
</ul></p>
</body>
</html>
最亲切的问候
答案 0 :(得分:0)
您在模板{{thing1}}
中使用转换为字典的字符串。
只需编写模板,根据需要格式化dict-entries:
%for thing1 in things1:
<li>'product':'{thing1.product}', Total_Product: {{thing1.Total_Product}}</li>
%end
或
%for thing1 in things1:
<li>'product':'{thing1.product}' 'Total_Product:{{thing1.Total_Product}}</li>
%end
您的问题不明确,您更喜欢哪一个。