将字典的字典导出到 CSV 文件会将内部值转换为不需要的字符串
我有一个词典,该字典是一个值,值是另一个键的词典,其各自的值为列表。
如下一个示例是密钥655值:
655: {
{
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: [],
8: [
[299, 0.4444444444444444, 0],
[627, 0.4444444444444444, 0],
[300, 0.2222222222222222, 0],
[628, 0.2222222222222222, 0],
[301, 0.1111111111111111, 0],
[629, 0.1111111111111111, 0],
[302, 0.2222222222222222, 0],
[630, 0.2222222222222222, 0]
],
9: []
}
}
我的问题是,当我导出字典intop a csv文件并从另一个脚本导入时,我会得到以下结果:
'655': [
'{0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [[299, 0.4444444444444444, 0], [627, 0.4444444444444444, 0], [300, 0.2222222222222222, 0], [628, 0.2222222222222222, 0], [301, 0.1111111111111111, 0], [629, 0.1111111111111111, 0], [302, 0.2222222222222222, 0], [630, 0.2222222222222222, 0]], 9: []}'
]
}
我已经对关键问题进行了整理,将其转换回int,但我不喜欢t知道如何将值恢复为字典,而不是将其恢复为字符串。
I have a dictionary that holds as values another dictionary of 10 keys with its respective values as list.
As an example following is key 655 values:
655: {
{
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: [],
8: [
[299, 0.4444444444444444, 0],
[627, 0.4444444444444444, 0],
[300, 0.2222222222222222, 0],
[628, 0.2222222222222222, 0],
[301, 0.1111111111111111, 0],
[629, 0.1111111111111111, 0],
[302, 0.2222222222222222, 0],
[630, 0.2222222222222222, 0]
],
9: []
}
}
My problem is that when I export the dictionary intop a csv file and imported from another script I get the following result:
'655': [
'{0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [[299, 0.4444444444444444, 0], [627, 0.4444444444444444, 0], [300, 0.2222222222222222, 0], [628, 0.2222222222222222, 0], [301, 0.1111111111111111, 0], [629, 0.1111111111111111, 0], [302, 0.2222222222222222, 0], [630, 0.2222222222222222, 0]], 9: []}'
]
}
I have already sort out the key issue converting it back to int but I don't know how to recover the value as dictionary, instead of getting it as a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据@enke的建议,以下内容为我的问题提供了解决方案。
首先,我将数据导入为:
然后,我将主字典中的键及其值重新转换回包含十个键及其值的字典(作为列表)。
获得具有所需结果的新字典:
}
Following @enke's suggestion the following provides a solution to my issue.
First I import the data as:
Then I reconvert the key from the main dictionary and its values back to a dictionary of ten keys and its values as list.
Obtaining a new dictionary with the desired result:
}