D
D
Dmitry Shevchenko2021-04-15 15:32:20
Python
Dmitry Shevchenko, 2021-04-15 15:32:20

Is the data written to json incorrectly?

good day, there is such a code

with open("base.json", "r+",encoding = "utf-8") as f:
            base = json.load(f)
            print (str(base))  //строчки что бы убедиться что я получаю то что надо
            print(type(base))  //строчки что бы убедиться что я получаю то что надо
            base["user"] = { "se" : 1}
            json.dump( base , f)

which apparently writes data to a file with the following content
{
  "user" : {}
}

in theory, he should assign another dictionary to the 'user' key, but I get the following file
{
  "user" : {}
}{"user": {"se": 1}}

Help me to understand!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-04-15
@ZER0x32

It is better to open a separate file for reading and writing

with open("base.json", "r",encoding = "utf-8") as f:
  base = json.load(f)

base["user"] = { "se" : 1}

with open("base.json", "w",encoding = "utf-8") as f:
  json.dump( base , f)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question