日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【文件处理】——字典写入json文件或TXT文件,读取文件中的字典TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法

發(fā)布時(shí)間:2023/12/10 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【文件处理】——字典写入json文件或TXT文件,读取文件中的字典TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

一、將字典寫入json文件

二、json文件中讀取字典

三、將字典寫入TXT文件中

四、從TXT中讀取字典

五、解決字典含數(shù)組存入json文件失敗的方法

1、存入前將數(shù)組變成列表

2、擴(kuò)展類方法


?


一、將字典寫入json文件

import jsontest_dict = {'version': "1.0",'explain': {'used': True,'details': "this is for josn test",} }#將字典轉(zhuǎn)換為字符串形式 json_str = json.dumps(test_dict, indent=4)#注意這個(gè)indent參數(shù),可以保存字典的縮進(jìn)格式,否則為一行with open('test_data.json', 'w') as json_file:json_file.write(json_str)

二、json文件中讀取字典

with open('test_data.json', 'r') as json_file:dic = json.load(json_file)

三、將字典寫入TXT文件中

import jsondic = { 'andy':{ 'age': 23, 'city': 'beijing', 'skill': 'python' }, 'william': { 'age': 25, 'city': 'shanghai', 'skill': 'js' } } js = json.dumps(dic) file = open('test.txt', 'w') file.write(js) file.close()

四、從TXT中讀取字典

import jsonfile = open('test.txt', 'r') js = file.read() dic = json.loads(js) print(dic) file.close()

五、解決字典含數(shù)組存入json文件失敗的方法

因?yàn)閖son無法序列化

1、存入前將數(shù)組變成列表

array.tolist()

2、擴(kuò)展類方法

class NpEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.ndarray):return obj.tolist()else:return super(NpEncoder, self).default(obj)

將上述代碼添加到你的代碼中,然后改成json.dumps(data, cls=NpEncoder)?

TypeError: Object of type 'ndarray' is not JSON serializable

from collections import defaultdict import json import numpy as npclass NpEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.ndarray):return obj.tolist()else:return super(NpEncoder, self).default(obj)video = defaultdict(list) video["label"].append("haha") video["data"].append(234) video["score"].append(0.3) video["label"].append("xixi") video["data"].append(123) video["score"].append(0.7)test_dict = {'version': "1.0",'results': (np.zeros((2,3))),'explain': {'used': True,'details': "this is for josn test",} } print(test_dict) json_str = json.dumps(test_dict, indent=4,cls=NpEncoder)#注意這個(gè)indent參數(shù) with open('test_data.json', 'w') as json_file:json_file.write(json_str)with open('test_data.json', 'r') as json_file:dic = json.load(json_file) print(type(dic))

參考:

https://blog.csdn.net/li532331251/article/details/78203438

https://blog.csdn.net/BobChill/article/details/83864285

總結(jié)

以上是生活随笔為你收集整理的【文件处理】——字典写入json文件或TXT文件,读取文件中的字典TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。