将{ "project": { "version": "4", "component": [ { "name": "ProjectRootManager", "output": { "url": "file://$PROJECT_DIR$/out" } }, { "@name": "ProjectModuleManager", "modules": { "module": { "@fileurl": "file://$PROJECT_DIR$/luqingya.iml", "@filepath": "$PROJECT_DIR$/luqingya.iml" } } } ] } }和{ "component": { "name": "ChangeListManager", "list": { "default": true, "id": "55bc6300-9d7c-4ac5-9546-1310814122e4", "name": "Default Changelist", "comment": "" }, "option": [ { "name": "SHOW_DIALOG", "value": false }, { "name": "HIGHLIGHT_CONFLICTS", "value": true }, { "name": "HIGHLIGHT_NON_ACTIVE_CHANGELIST", "value": false }, { "name": "LAST_RESOLUTION", "value": "IGNORE" } ] } }写入一个文件
时间: 2023-07-02 08:03:55 浏览: 152
你可以将以上两段 JSON 数据写入一个文件中,例如命名为 `config.json`,示例代码如下:
```python
import json
data1 = {
"project": {
"version": "4",
"component": [
{
"name": "ProjectRootManager",
"output": {
"url": "file://$PROJECT_DIR$/out"
}
},
{
"@name": "ProjectModuleManager",
"modules": {
"module": {
"@fileurl": "file://$PROJECT_DIR$/luqingya.iml",
"@filepath": "$PROJECT_DIR$/luqingya.iml"
}
}
}
]
}
}
data2 = {
"component": {
"name": "ChangeListManager",
"list": {
"default": true,
"id": "55bc6300-9d7c-4ac5-9546-1310814122e4",
"name": "Default Changelist",
"comment": ""
},
"option": [
{
"name": "SHOW_DIALOG",
"value": false
},
{
"name": "HIGHLIGHT_CONFLICTS",
"value": true
},
{
"name": "HIGHLIGHT_NON_ACTIVE_CHANGELIST",
"value": false
},
{
"name": "LAST_RESOLUTION",
"value": "IGNORE"
}
]
}
}
with open('config.json', 'w') as f:
json.dump([data1, data2], f)
```
注意:由于以上两段 JSON 格式不同,因此将它们作为列表 `[data1, data2]` 写入文件中。在读取时也需要先读取整个列表,再对其中的数据进行解析。
阅读全文