-1

when I want to write "සිවු අවුරුදු පාටමාලාව" with the exact wording into a json file using python3.6, but instead \u0dc3\u0dd2\u0dc3\u0dd4\u0db1\u0dca\u0da7 \u0dc3\u0dd2\u0dc0\u0dd4 is written into the json file.

I read an excel using xlrd and write to using open().

import xlrd 
import json

wb = xlrd.open_workbook('data.xlsx',encoding_override='utf-8') 
sheet = wb.sheet_by_index(0) 

with open('data.json', 'w') as outfile:
    data = json.dump(outerdata,outfile,ensure_ascii=True)
martineau
  • 119,623
  • 25
  • 170
  • 301
erandi
  • 317
  • 1
  • 7
  • 18
  • Possible duplicate of [Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence](https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence) – tripleee Feb 16 '19 at 11:03

1 Answers1

2

If I do this in Python with the escape string you report:

>>> print ("\u0dc3\u0dd2\u0dc3\u0dd4\u0db1\u0dca\u0da7 \u0dc3\u0dd2\u0dc0\u0dd4")
සිසුන්ට සිවු

you will see that the escapes do render as the characters you want. These are two different representations of the same data. Both representations are valid in JSON. But you are using json.dump() and you have specified ensure_ascii=True. That tells json.dump() that you want the representation with escapes. That is what ascii means: only the printable characters between chr(32) and chr(126). Change that to ensure_ascii=False.

But because you are now no longer writing pure ascii to your output file data.json, you need to specify an encoding when you open it:

with open("data.json", "w", encoding="utf-8") as outfile:
    data = json.dump(outerdata,outfile,ensure_ascii=False)

This will make your JSON file look the way you want it to look.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • When i set ensure_ascii=False then it throws an error. UnicodeEncodeError: 'charmap' codec can't encode characters in position 3-8: cha racter maps to – erandi Feb 16 '19 at 17:11
  • 1
    Set the encoding of the file to something other than the default, which is ascii. See edit. – BoarGules Feb 16 '19 at 17:51
  • thanks @BoarGules ,i tried to achieve the result many days.you saved me... – erandi Feb 16 '19 at 23:03