I am stuck with a problem, indeed I have a JSON file in which each objects is in a line. So, if there are 100 objects, there will be 100 lines.
[{ "attribute1" : "no1", "attribute1": "no2"}
{ "attribute1" : "no12", "attribute1": "no22"}]
I open this JSON file, and delete some atttributes of every elements.
Then, I want to write the objects back into the file in the same way (1 object = 1 line).
I have tried to do so with "indent" and "separators" but it does not work.
I would like to have :
[{ "attribute1": "no2"}
{"attribute1": "no22"}]
Thanks for reading.
with open('verbes_lowercase.json','r+',encoding='utf-8-sig') as json_data:
data=json.load(json_data)
for k in range(len(data)):
del data[k]["attribute1"]
json.dump(data,json_data,ensure_ascii=False , indent='1', separators=(',',':'))
json_data.seek(0)
json_data.truncate()
[{},{}]
as notation, then you have a valid json file to load.\n
is indeed for line breaks. If you use windows you should use\r\n
, Unix systems usually only need\n