gpt4 book ai didi

python - 如何保存有元组键的字典

转载 作者:行者123 更新时间:2023-12-01 21:56:44 26 4
gpt4 key购买 nike

我想保存键上有元组的字典。

我尝试了 pickle、ujson、json 来保存字典。所有这些都不起作用。

字典有:

dictionary = {(-30, -29, -72, -71): [[-29.99867621124457, -71.75349423197208, 220], [-29.996964568219873, -71.7521560207641, 220], [-29.99696437241995, -71.7507330056961, 220], [-29.99761665426199, -71.75016101067708, 220]]}

我试过:

with open('depth_echo.txt', 'w') as file:
file.write(ujson.dumps(dictionary)

import json
a, b, c = "abc"
data = {(1,2,3):(a,b,c), (2,6,3):(6,3,2)}
on_disk = json.dumps(data.items())

最佳答案

将字典写成字符串

 with open(r'test.txt','w+') as f:
f.write(str(dictionary))

使用eval读取

dic = ''
with open(r'test.txt','r') as f:
for i in f.readlines():
dic=i #string
dic = eval(dic) # this is orignal dict with instace dict

关于python - 如何保存有元组键的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56403013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com