Apr-21-2022, 03:15 PM
Hi,
I need your help, please. I'm french. Sorry for my bad english !
I want to append a file with json format but I want to have line carrier return in the file to simplify read of the file.
I have a file with json records.
example :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}]
I want to append the file with a new record :
addjson = {"surname" : "aaa", "forename" : "Paul", "age" : 40}
The code after gives :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}, {"surname" : "aaa", "forename" : "Paul", "age" : 40}]
but I have several problems :
1/ I would like to append the file with "addjson" record. At the moment, my code delete file and create a new file. How to append file with a new information ?
2/ I would like to format my json file in another form :
ListeCharacters.json =
[
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]
It's to simplfy reading of file. Is it allowed with json format ? how to do this ?
Thanks a lot for your help.
############################################################################\n",
I need your help, please. I'm french. Sorry for my bad english !
I want to append a file with json format but I want to have line carrier return in the file to simplify read of the file.
I have a file with json records.
example :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}]
I want to append the file with a new record :
addjson = {"surname" : "aaa", "forename" : "Paul", "age" : 40}
The code after gives :
ListeCharacters.json =
[{"surname" : "xxx", "forename" : "jean", "age" : 24}, {"surname" : "yyy", "forename" : "paul", "age" : 31}, {"surname" : "zzz", "forename" : "julia", "age" : 28}, {"surname" : "aaa", "forename" : "Paul", "age" : 40}]
but I have several problems :
1/ I would like to append the file with "addjson" record. At the moment, my code delete file and create a new file. How to append file with a new information ?
2/ I would like to format my json file in another form :
ListeCharacters.json =
[
{"surname" : "xxx", "forename" : "jean", "age" : 24},
{"surname" : "yyy", "forename" : "paul", "age" : 31},
{"surname" : "zzz", "forename" : "julia", "age" : 28},
{"surname" : "aaa", "forename" : "Paul", "age" : 40}
]
It's to simplfy reading of file. Is it allowed with json format ? how to do this ?
Thanks a lot for your help.

############################################################################\n",
fileName = "ListeCharacters.json" try: with open(fileName) as json_file: liste = json.load(json_file) liste.append(addjson) except IOError: liste = [addjson] with open(fileName, "w") as outfile: json.dump(liste, outfile)