db = MySQLdb.connect(host='数据库,user=用户名,passwd=密码,db='数据库, init_command="set names utf8" )
3. 查询出来的数据转化为json
t = json.dumps(r, ensure_ascii=False)
注意: ensure_ascii=False
python 的api是这样解释的:
If ensure_ascii is False (default: True), then some chunks written to fp may be unicode instances, subject to normal Python str to unicode coercion rules. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.
PS : e文不好 就不再这里误导了。
如果 不加 ensure_ascii=False 输出的 t 如果有汉字的话都默认给转换成一堆编码 如果加上的话 就都能正常显示变成了汉字
例如:
不加的话: t = json.dumps(r)