【发布时间】:2023-01-13 04:37:46
【问题描述】:
首先,这是我的 JSON 文件结构
[{
"title": "Reference Addition",
"ref_date": 20200110,
"country": "ASIA",
"ref_internal": "1",
"ref_external": "1"
}]
我有在 Python 中成功加载文件的代码。我想更改国家/地区的值并将其保存到新文件中。
with open('myfile.json', 'r') as f:
json_data = json.load(f)
json_data['country'] = 'AFRICA'
with open('myfile.json', 'w') as f:
json.dump(json_data, f, indent=2)
但不幸的是我不断得到
AttributeError: module 'json' has no attribute 'tree'
在网上搜索了一些东西,之后我设法解决了那个错误,但现在遇到了这个错误
import json
myfile = ('JSON\TRADE.json')
with open (myfile, 'r') as myfile: json_data = json.load(myfile) json_data['country'] = 'AFRICA'
json.tree.dump(json_data, indent=4)
with open(myfile, 'w') as f: json.dump(json_data, f, indent=4)
现在带有完整回溯的错误是
追溯(最近一次通话):
File "c:\AUTOMATION\Data Creation\JSON\EDIT.py", line 7, in json_data['country'] = 'AFRICA' TypeError: list indices must be integers or slices, not str PS C:\AUTOMATION\Data Creation>
如果有任何细节不正确,我们深表歉意,但请告诉我,以便我提供
【问题讨论】: