Answer the question
In order to leave comments, you need to log in
How to read data from json file and write it to the same file?
Greetings.
There is a json file. It is necessary to read the data in it, add new ones and write to the same file. I can do all this separately, but I can't do it within one structure with open(file)
. Please share your solution.
Answer the question
In order to leave comments, you need to log in
First read, add the necessary data to the object and write. The code will "stretch" into two lines, but it will be clear what is happening.
with open('file.txt', 'r+') as f:
data = json.load(f)
data.append(4)
f.seek(0)
json.dump(data, f)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question