不是最有效的,但能够完成任务,如果你想只通过已经呈现为HTML作为表变量的视图。更好的方法是只传递数据,然后让模板使用模板逻辑循环并在所需位置输出变量。
data = {'sentiment_analysis_result': [{'article_title': u'These Digital Locks Help Keep Tabs on Tenants', 'score': u'0.139613', 'type': u'positive'}, {'article_title': u'You Can Get a $50 Phone From Amazon, If You Don\u2019t Mind the Ads', 'score': u'0.239663', 'type': u'positive'}]}
table_string = '
for key, value in data.iteritems():
table_string += ''
table_string += '
' + key + ''table_string += '
'table_string += '
'for i, d in enumerate(value):
if i == 0:
table_string += '
'for k in d.iterkeys():
table_string += '
' + k + ''table_string += '
'table_string += '
'for v in d.itervalues():
table_string += '
' + v + ''table_string += '
'table_string += '
'table_string += '
'print(table_string)