在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"
)
|
def 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)
知识点:
1.上面的json强制编码转义。
2.Google Charts。图表工具,太强大了。https://google-developers.appspot.com/chart/interactive/docs/gallery/columnchart
3.排序功能的强大性。http://ghostfromheaven.iteye.com/blog/1563576
students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
sorted(students, key=lambda s : s[2])
4.