python中numpy存json 出错:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

原本写法: 

json_data ={}
#tmp:[24,3,3]
for i in range(tmp.shape[0]):
    json_data[str(i)] = tmp[i].tolist()
f = open("new.json",'w')
json.dump(json_data,f,indent=2,ensure_ascii=False)


with open("new.json",'r') as tf:
    new_dict = json.load(tf)

正确写法:(一定记得用f = open("xx.json",'w') 后f.close()一下,或者直接用with 管理不用手动close)不然,文件句柄没有被释放,立即读取的new.json是个空文件

json_data ={}
#tmp:[24,3,3]
for i in range(tmp.shape[0]):
    json_data[str(i)] = tmp[i].tolist()
with open("new.json",'w') as f
    json.dump(json_data,f,indent=2,ensure_ascii=False)


with open("new.json",'r') as tf:
    new_dict = json.load(tf)

你可能感兴趣的:(杂货,python,python,numpy,json)