I'm desesperately trying to send over tcp (Python 2.7) a json string that is utf8-encoded. Here are a few tries and the results. The variable reponse contains the json string I'm trying to send:
reponse = {"candidats":{"P":[{"mentionname":"Beyoncé","guess":[{"name":"BEYONCÉ","score":"1.00","eid":"72437"}]}],"E":[]}}
The command 1:
self.request.sendall(json.dumps(reponse+"\n",ensure_ascii=False))
results in error:
'ascii' codec can't encode character u'\xe9' in position 49: ordinal not in range(128)
The command 2:
self.request.sendall(json.dumps(reponse+"\n",encoding='utf8')):
gives an output at the other end (tcp client) but last character of Beyoncé is not the good one :
"{\"candidats\":{\"P\"[{\"mentionname\":\"Beyonc\u00e9\",\"guess\":[{\"name\":\"BEYONC\u00c9\",\"score\":\"1.00\",\"eid\":\"72437\"}]}],\"E\":[]}}\n"
(message is received in the client with message.decode('UTF-8')).
The command 3:
self.request.sendall(json.dumps(reponse+"\n",ensure_ascii=False,encoding='utf8')):
results in error:
'ascii' codec can't encode character u'\xe9' in position 49: ordinal not in range(128)
The command 4:
self.request.sendall(json.dumps(reponse+"\n").encode('utf8')):
gives an output at the other end (tcp client) but last character of Beyoncé is not the good one:
"{\"candidats\":{\"P\":[{\"mentionname\":\"Beyonc\u00e9\",\"guess\":[{\"name\":\"BEYONC\u00c9\",\"score\":\"1.00\",\"eid\":\"72437\"}]}],\"E\":[]}}\n"
The command 5:
self.request.sendall(json.dumps(reponse+"\n",ensure_ascii=False).encode('utf8')):
gives an output at the other end, last character of Beyoncé is the good one but double quote are escaped:
"{\"candidats\":{\"P\":[{\"mentionname\":\"Beyoncé\",\"guess\":{\"name\":\"BEYONCÉ\",\"score\":\"1.00\",\"eid\":\"72437\"}]}],\"E\":[]}}\n"
Last try is almost the good one, except for those annoying escaped double quotes. I know that this is because string is double encoded but I have no other choice for the moment to choose this solution and eliminate backslashes in my tcp client code.
Does anybody have a better solution? Any hint is greatly appreciated! Regards, Patrick
coding:utf8
piece was already there. I thought you were saying to remove it. – Patrick Feb 04 '15 at 18:52