天天看点

python把字典转换为html,Python字典在flas中转换为HTML

这不是最有效的方法,但是如果您想将已经呈现为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)