0

I try to edit a json file (encoded in utf-8, says notepad++), but all the french accent inside are destroyed doing it. I cannot find any help with this problem on internet, could somebody help me ? Getting a little crazy..

my json file is test.json and it has only one line : {"old": "bât_1"}

I try :

# -*- coding: utf-8 -*-
import json
newdict = {'new':'bât_2'}
with open('test.json') as inputdata:
    data = json.load(inputdata, encoding = 'utf-8')
data.update(newdict)
with open('test.json', 'w', encoding = 'utf-8') as fid:
    json.dump(data, fid)

and if I open again test.json, I get: {"old": "b\u00c3\u00a2t_1", "new": "b\u00e2t_2"}

Thank you for any help

  • Use `ensure_ascii=False` in `json.dump`. – Mark Tolonen Dec 15 '20 at 07:51
  • trying : ```with open('test.json', 'w', encoding = 'utf-8') as fid: json.dump(data, fid, ensure_ascii=False)``` I get : {"old": "bât_1", "new": "bât_2"} still not what I want. I need : {"old": "bât_1", "new": "bât_2"} – Alan Lauraux Dec 17 '20 at 14:50
  • Your old data wasn’t written as utf8. Did you create it with an editor and not save it in utf8 encoding? Write it the same way as the new data. – Mark Tolonen Dec 17 '20 at 16:13
  • I'm using notepad++, I did convert the json to utf-8, and saved it, so I don't know what I could do more.. – Alan Lauraux Dec 18 '20 at 09:35

0 Answers0