Get unicode text when Python encode utf-8 -


i use wechat public platform send content. , code is:

# coding:utf-8 import os import urllib2 import json import httplib2)      content = "一些中文"  body = {     "touser":"abcdefg",     "msgtype":"text",     "text":     {             "content": content     } }  access_token = '1234567890' req_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' + access_token method = 'post' headers['content-type'] = 'application/json' headers['accept-charset'] = 'encoding=utf-8' resp, token = http_obj.request(req_url, method, headers=headers, body=json.dumps(body)) 

i receive \u4e00\u4e9b\u4e2d\u6587 not when run 一些中文 program. should if want receive 一些中文?? lot !

you can dump body following:

json.dumps(body, ensure_ascii=false) 

from python docs (https://docs.python.org/2/library/json.html):

if ensure_ascii true (the default), non-ascii characters in output escaped \uxxxx sequences, , result str instance consisting of ascii characters only. if ensure_ascii false, chunks written fp may unicode instances. happens because input contains unicode strings or encoding parameter used. unless fp.write() explicitly understands unicode (as in codecs.getwriter()) cause error.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -