简体   繁体   English

Bottle,Python和MongoDB中的JSON输出

[英]JSON output in Bottle, Python and MongoDB

I am running Bottle and python to pull my data from MongoDB. 我正在运行Bottle和python从MongoDB中提取数据。 my output comes in JSON object format. 我的输出采用JSON对象格式。
I used to have the following 我曾经有以下

{u'product': u'mortgage', u'Total_Product': 146533}
{u'product': u'debt collection', u'Total_Product': 65639}

but wanted to get rid of the 'u and get the following format: 但想摆脱'u并获取以下格式:

'product':'mortgage', Total_Product: 146533
'product':'debt collection' 'Total_Product:65639

After running this code, I am left with a blank screen with no results and no error message. 运行此代码后,我没有显示任何结果,也没有错误消息的空白屏幕。 Any suggestion? 有什么建议吗?

# 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)

the tpl is as follows: 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>

kindest regards 最亲切的问候

You are using the convertion to a string of a dict in your template {{thing1}} . 您正在模板{{thing1}}中使用到字典字符串的转换。

Just write your template, to format the dict-entries as you want: 只需编写您的模板,即可根据需要格式化字典条目:

%for thing1 in things1:
<li>'product':'{thing1.product}', Total_Product: {{thing1.Total_Product}}</li>
%end

or 要么

%for thing1 in things1:
<li>'product':'{thing1.product}' 'Total_Product:{{thing1.Total_Product}}</li>
%end

your question is not clear, which one you prefer. 您的问题不清楚,您更喜欢哪一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM