把列表中的unicode轉成中文。用於jinja2中的渲染
阿新 • • 發佈:2019-02-17
在python 下面一個包含中文字串的列表(list)或字典,直接使用print會出現以下的結果:
1 2 3 |
dict = { "asdf" : "我們的python學習" }
print dict
{ 'asdf' : '\xe6\x88\x91\xe4\xbb\xac\xe7\x9a\x84python\xe5\xad\xa6\xe4\xb9\xa0' }
|
在輸出處理好的資料結構的時候很不方便,需要使用以下方法進行輸出:
1 2 |
import simplejson
print simplejson.dumps( dict , encoding = "UTF-8" , ensure_ascii = False )
|
1 |
{ "asdf" : "我們的python學習" }
|
注意上面的兩個引數
另外一個方法:
1 2 |
import json
print json.dumps.( dict ).decode( "unicode-escape" )
|
原文:http://my.oschina.net/u/197384/blog/192292 知識點: 1.上面的json強制編碼轉義。 2.Google Charts。圖表工具,太強大了。https://google-developers.appspot.com/chart/interactive/docs/gallery/columnchart 3.排序功能的強大性。http://ghostfromheaven.iteye.com/blog/1563576def activity_analysis(): info = [[u'學校',u'學生數量']] for school_pair in config.SCHOOLS[:-1]: school = school_pair[0] number = PaticipatorInfo.query.filter_by(school=school).count() info.append([school,int(number)]) print info,type(school),type(number) import json info = json.dumps(info).decode("unicode-escape") return render_template('activity/activity_analysis.html',info=info)
4.students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)] sorted(students, key=lambda s : s[2])