-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)

6
  • 1
    Python 2 or 3? Where does the data come from? What's the code that's handling it? Commented Jun 22, 2015 at 9:55
  • @deceze - python 2.7.6 The data is coming from a Thai website. I was scraping it for some work. Commented Jun 22, 2015 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)') Commented Jun 22, 2015 at 10:03
  • I recommend looking at this: stackoverflow.com/questions/1885181/… Commented Jun 22, 2015 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',) Commented Jun 22, 2015 at 10:10

1 Answer 1

1

Just use:

json.dumps(your_string, ensure_ascii=False)

This will disable escaping non-ascii characters.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.