0

im trying to sending a response to a web, with multilingual characters on python 3 but all the time, it comes this: "\\xd8\\xa7\\xd9\\x84\\xd9\\x82\\xd8\\xa7\\xd9\\x85\\xd9\\x88\\xd8\\xb3 \\xd8\\xa7\\xd9\\x84\\xd8\\xb9\\xd8\\xb1\\xd8\\xa8\\xd9\\x8a Espa\\xc3\\xb1a". When the correct answer is this:

القاموس العربي España.

This is the code:

s="القاموس العربي España".encode(encoding='UTF-8')

Where can be my mistake?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
hidura
  • 681
  • 3
  • 11
  • 36
  • I think `.encode(encoding='UTF-8')` is not needed. In python 3, strings are unicode by default. – Marcin Dec 30 '14 at 03:23
  • Is by default but since it's not working in the normal way i put it in that way. – hidura Dec 30 '14 at 03:24
  • u use windows? not sure windows console can handle unicode. – Marcin Dec 30 '14 at 03:25
  • No, I am using Linux. – hidura Dec 30 '14 at 03:28
  • Same here. And without `.encode(encoding='UTF-8')` I get correct results as shown here: http://i.imgur.com/gnXQTkQ.jpg – Marcin Dec 30 '14 at 03:31
  • The problem is that i need to encode the text, to send it to a web, and on the Web is returning like this. – hidura Dec 30 '14 at 03:37
  • 3
    @hidura, how are you setting the `Content-Type` header? `Content-Type: text/html; charset=utf-8` should work fine with sending the `utf8`-encoded bytes over to the browser. – Alex Martelli Dec 30 '14 at 03:42
  • I'm little confused if it is talking about sending a web **response** to somewhere else, or sending a **request**? – Anzel Dec 30 '14 at 03:45
  • I am sending the response, the request comes perfect. The response is the problem, and yes i have it like that! – hidura Dec 30 '14 at 03:49
  • Ah ok. your question was not specific that you are talking about web development. – Marcin Dec 30 '14 at 03:52
  • @hidura, can you provide some code how you actually send the response (on django, socket or etc.)? It's quite hard to understand what goes wrong (or needs attention) without a reproducible sample. – Anzel Dec 30 '14 at 03:55
  • I found it! It was a mess with the JSON responser, that i was writing with ensure_ascii=True, and the response was trying to send it as a JSON not as HTML. Thanks anyway. – hidura Dec 30 '14 at 04:08
  • 1
    @hidura Why don't you answer your own question and close this question as answered – Bhargav Rao Dec 30 '14 at 17:40

1 Answers1

0

I found it! It was a mess with the JSON responser, that i was writing with ensure_ascii=True, and the response was trying to send it as a JSON not as HTML. By using the ensure_ascii=True the system will print correctly any JSON answer.

hidura
  • 681
  • 3
  • 11
  • 36