D
D
Dmitry Shevchenko2020-10-22 15:15:49
Python
Dmitry Shevchenko, 2020-10-22 15:15:49

Error while writing json to file?

read about json , from there I realized that they use

with open("file.json", 'w+') as f :
                             cl = {}
                             json.dump(cl, f)
here is my case:
if not str(message.chat.id) in os.listdir('mat/'):
                        os.mkdir('mat/'+str(message.chat.id))
                        with open("mat/" + str(message.chat.id) + "/users_mat.json", 'w+') as f :
                             cl = {}
                             json.dump(cl, f)
                             print('111111111111')
                    f = open("mat/" + str(message.chat.id) + "/users_mat.json", 'w+')
                    users_mat =json.load(f)
                    if not str(message.from_user.id) in users_mat.keys():
                        users_mat[str(message.from_user.id)]: 0
                        json.dump(users_mat , f)
                    users_mat[str(message.from_user.id)]: users_mat [str(message.from_user.id)]+1
                    json.dump(users_mat ,f)

but the error : raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
swears at my attempt to save the dictionary to a file in the first if.
I do not rule out that I did not correctly understand the meaning of "dump", for the first time in my life I work with json, and all sites say the same thing, and there is no one about working with files.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-10-22
@ZER0x32

f = open("mat/" + str(message.chat.id) + "/users_mat.json", 'w+')
users_mat =json.load(f)

Opened the file in write mode -> deleted the contents of the file -> trying to read json from an empty file
Actually, change the file open mode from w+tor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question