PythonTip >> 博文 >> chinaunix

python josn格式中文输出乱码问题

zihua 2014-01-13 18:01:33 点击: 724 | 收藏


import urllib
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )


RCMC_URL = "http://192.168.100.211:xxxx/xxx-api/xxxx"


RCMS_URL_response = urllib.urlopen(RCMC_URL).read()
json_obj = json.loads(RCMS_URL_response)
list_rcms = []
for channels in json_obj:
    CID = channels["channelCode"]
    BU_name = channels["bu"]
    list_rcms.append({"channel_id":CID,"BU":BU_name})
josn_rcms = json.dumps(list_rcms,ensure_ascii=False)


with open("rcms_channels.txt","w")as f:
    f.write(josn_rcms)

红色标注的是需要添加的,例子中要输出的是json_rcms的结果,其中有中文字符;
原文链接:http://blog.chinaunix.net/uid-12014716-id-3892081.html

作者:zihua | 分类: chinaunix | 标签: python | 阅读: 724 | 发布于: 2014-01-13 18时 |