D
D
Dmitry Shevchenko2020-11-17 16:52:58
Python
Dmitry Shevchenko, 2020-11-17 16:52:58

Expecting value: line 1 column 1 (char 0) with banal json.load()?

here in this piece of code I load json file

def set_mat(id):  
            global users_mat
            if not str(id) in os.listdir('mat/'):
                os.mkdir(str('mat/'+str(id)))
                with open("mat/" + str(id) + "/users_mat.json", 'w+') as f:
                    json.dump({'mats':{},'send_time' : {'hour': 9, 'min': 0, 'is_on': 1}, 'last_time':{'day':time.strptime(time.asctime()).tm_yday-1}}, f)
            with open("mat/" + str(id) + "/users_mat.json", 'r') as f:
                users_mat = json.load(f)
                print (str(json.load(f)))
            return users_mat
        def mat_filter(message):  #
            def mat_write(message, word):
                set_mat(message.chat.id)
                for mat in cfg.mat_list:
                    if mat.lower() in word.lower():
                        if not str(message.from_user.id) in users_mat['mats'].keys():
                            with open("mat/" + str(message.chat.id) + "/users_mat.json", 'w+') as f:
                                users_mat['mats'][str(message.from_user.id)] = 0
                                json.dump(users_mat, f)
                with open("mat/" + str(message.chat.id) + "/users_mat.json", 'w+') as f:
                    users_mat['mats'][str(message.from_user.id)] = users_mat['mats'][str(message.from_user.id)] + 1
                    json.dump(users_mat, f)
            wordm = ''
            x = 0
            for i in message.text:
                x += 1
                if x == len(message.text):
                    mat_write(message, wordm)
                    wordm = ''
                    break
                if i == '\n':
                    continue
                if i != ' ':
                    wordm += i

                else:
                    mat_write(message, wordm)
                    wordm = ''
I call the mat_filter function, which in turn calls set_mat , inside which I create a jane file of the format {'hour': 9, 'min': 0, 'is_on': 1}, 'last_time':{'day':time .strptime(time.asctime()).tm_yday-1}} , and then I read it but when reading (json.load()) an error occurs:
020-11-17 16:48:36,384 (util.py:75 WorkerThread1 ) ERROR - TeleBot: "JSONDecodeError occurred, args=('Expecting value: line 1 column 1 (char 0)',)
Traceback (most recent call last):
File "C:\Users\abraham\AppData\Local\Programs\ Python\Python38-32\lib\site-packages\telebot\util.py", line 69, in run
task(*args, **kwargs)
File "C:/Users/abraham/Desktop/...", line 186, in reaction
mat_filter(message)
File "C:/Users/abraham/Desktop/...", line 46, in mat_filter
mat_write(message, wordm)
File "C:/Users/abraham/Desktop/...", line 31, in mat_write
set_mat( message.chat.id)
File "C:/Users/abraham/Desktop/...", line 27, in set_mat
print (str(json.load(f)))
File "C:\Users\abraham\AppData\ Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\abraham\AppData\Local\Programs\Python\ Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\abraham\AppData\Local\Programs\Python\Python38-32\lib\json\ decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\abraham\AppData\Local\Programs\Python\Python38-32\lib\json\decoder. py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

and yes, I'm sure that the condition to create file passes, since I specifically delete it! after error - file remains empty...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-11-17
@ZER0x32

users_mat = json.load(f)
print (str(json.load(f)))

Read the file to the end, parse it, and then try to print the result of parsing the end of the file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question