simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
提示说是解码错误
可以用下面的方法判断json文件是否为空,即使为空,读取内容也不会报错
import json
#读取with open(‘tmp.json‘, ‘r‘) as f: ???data = f.read()if(not bool(data)): ???print("json is empty!")
非空情况下也可以读取数据
import jsondata ={"vf":"ff"}
#写入with open(‘tmp.json‘, ‘w‘) as f: ???json.dump(data, f)#读取with open(‘tmp.json‘, ‘r‘) as f: ???data = f.read() ???print(data)
#判断if(not bool(data)): ???print("json is empty!")
json文件为空时读取会报错
原文地址:https://www.cnblogs.com/sea-stream/p/9998784.html