python - JSON 文件格式的字典
问题描述
我有一个列表字典,我想以特定格式存储在 JSON 文件中。
格式:
{ "type" :
{"name" : "Predicted", "inward" : "is predicted by", "outward" : "predicts"},
"inwardIssue" : { "key" : issues_key[1]},
"outwardIssue":{"key":data2}
}
字典中的数据:
{'CLOV-1984': ['CLOV-1979'], 'CLOV-1979': ['CLOV-1984'], 'CLOV-1970': ['CLOV-1951'], 'CLOV-1951': ['CLOV-1970'], 'CLOV-1910': ['CLOV-1909'], 'CLOV-1909': ['CLOV-1910'], 'CLOV-1853': ['CLOV-1849', 'CLOV-1845', 'CLOV-1842'], 'CLOV-1849': ['CLOV-1853', 'CLOV-1840'], 'CLOV-1845': ['CLOV-1853', 'CLOV-1840'], 'CLOV-1842': ['CLOV-1853', 'CLOV-1840'], 'CLOV-1840': ['CLOV-1849', 'CLOV-1845', 'CLOV-1842']}
我不断收到的错误是:
TypeError Traceback (most recent call last)
<ipython-input-62-76678d843e45> in <module>
8 {"name" : "Predicted", "inward" : "is predicted by", "outward" : "predicts"},
9 "inwardIssue" : { "key" : issues_key[1]},
---> 10 "outwardIssue":{"key":data2}
11 }
12 })
TypeError: unhashable type: 'dict'
我是 Python 新手,正在尝试不同的东西。
import json
data = {}
data2 = []
data2 = dictIssues[issues_key[1]]
print(data2)
data['CLOV-1985'] = []
data['CLOV-1985'].append({
{ "type" :
{"name" : "Predicted", "inward" : "is predicted by", "outward" :
"predicts"},
"inwardIssue" : { "key" : issues_key[1]},
"outwardIssue":{"key":data2}
}
})
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
我应该创建一个循环,遍历字典中的所有键并获取列表并将其放入 JSON 文件的格式中。
解决方案
推荐阅读
- azure-devops - Configuration of the PR completion comment "Completing Pull Request xxxxx and the associated work items"
- node.js - 在 mongodb 和 nodejs 中搜索文本并返回其相对路径?
- python - 将 json 文件编码为字节
- php - Laravel 在我的 script.js 中返回一个带有数据而不是响应 json 的 html 页面
- sql - 总和不匹配 在 MariaDB 上使用 sql 查询
- python - 有一个计费页面,我填写了所有详细信息,然后单击 make bill 按钮,然后应该会生成发票页面
- javascript - 需要 javascript react 中给定 URL 的完整呈现文本
- bootstrap-5 - Bootstrap 5:offcanvas,如何在鼠标离开时关闭它?
- c# - 我无法从 Visual Studio 在 PowerApps 数据库中创建实体
- python - 在训练 word2vec 模型 python 时,必须提供 corpus_file 或 corpus_iterable 值之一