-1

I have strings like these - Trang chủ and Đồ Dùng Nhà Bếp which have special charaters. When i print them, they are shown as it is. But when I convert it into Json, it changes to Trang ch\xe1\xbb\xa7. How can I print them as they are in JSON format also? Thanks in advance.

I tried the suggested answer of -

string.encode('utf-8', "ignore")

string.decode("ascii", "ignore")

and got this error:

UnicodeDecodeError('ascii', 'Trang ch\xe1\xbb\xa7', 8, 9, 'ordinal not in range(128)')

Is there a way around?

The link provided as duplicate is not the question I was asking.

The answer provided does solve my question : json.dumps(your_string, ensure_ascii=False)

Dreams
  • 5,854
  • 9
  • 48
  • 71
  • 1
    Python 2 or 3? Where does the data come from? What's the code that's handling it? – deceze Jun 22 '15 at 09:55
  • @deceze - python 2.7.6 The data is coming from a Thai website. I was scraping it for some work. – Dreams Jun 22 '15 at 10:03
  • @sagar - I tried the suggested answer of - string.encode('utf-8', "ignore") string.decode("ascii", "ignore") and got this error - UnicodeDecodeError('ascii', 'Trang ch\xe1\xbb\xa7', 8, 9, 'ordinal not in range(128)') – Dreams Jun 22 '15 at 10:03
  • I recommend looking at this: http://stackoverflow.com/questions/1885181/how-do-i-un-escape-a-backslash-escaped-string-in-python – Petzku Jun 22 '15 at 10:03
  • @Petzku - Thanks. But when I tried store = ast.literal_eval(store). Here store is dictionary. I get an error - ValueError('malformed string',) – Dreams Jun 22 '15 at 10:10
  • http://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte – Sagar Naliyapara Jun 23 '15 at 03:19

1 Answers1

1

Just use:

json.dumps(your_string, ensure_ascii=False)

This will disable escaping non-ascii characters.

ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46