瓶子、Python和MongoDB中的JSON输出

瓶子、Python和MongoDB中的JSON输出,python,mongodb,pymongo,bottle,Python,Mongodb,Pymongo,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'

我正在运行瓶子和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>

最亲切的问候

您正在使用转换为模板{{thing1}中的dict字符串

只需编写模板,即可根据需要格式化dict条目:

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


你的问题不清楚,你更喜欢哪一个。

你是想摆脱美国的吗?如果是这样的话,它只是将其表示为一个unicode字符串。是的,我正在尝试摆脱u的@riotburnok,我正在查看它@riotburnAnalytics.tpl是什么?
%for thing1 in things1:
<li>'product':'{thing1.product}' 'Total_Product:{{thing1.Total_Product}}</li>
%end