对于创建数据库的项目,我想使用python将.json文件转换为.sqlite3文件(目前在Windows 10上运行Python 3.6.4)。下面是用于读取json文件的代码
...
with open('C:/Documents/{}/Posts.json'.format(forum), encoding="utf8") as f:
row = json.load(f)
parent_id = row['_Id']
body = format_data(row['_Body'])
score = row['_Score']
comment_id = row['_Id']
comment_id_type = row['_PostTypeId']
parent_id_type = row['_PostTypeId']
accepted_answer_id = row['_AcceptedAnswerId']
accepted_parent_id = row['_ParentId']
...
运行此代码时遇到此错误。
File "C:\Python\data base.py", line 85, in <module>
parent_id = row['_Id']
KeyError: '_Id'
我已经读过这个错误发现,根据官方python文档,异常KeyError 是
在现有密钥集中找不到映射(字典)密钥时引发。
现在我无法理解这种语法,因为json文件中存在'_Id'(如下所示)
{
"posts": {
"row": [
{
"_Id": "1",
"_PostTypeId": "1",
"_AcceptedAnswerId": "3",
"_CreationDate": "2016-08-02T15:39:14.947",
"_Score": "5",
"_ViewCount": "254",
"_Body": "<p>What does \"backprop\" mean? I've Googled it, but it's showing backpropagation.</p>\n\n<p>Is the \"backprop\" term basically the same as \"backpropagation\" or does it have a different meaning?</p>\n",
"_OwnerUserId": "8",
"_LastEditorUserId": "7488",
"_LastEditDate": "2017-05-28T13:48:02.003",
"_LastActivityDate": "2017-05-28T13:48:02.003",
"_Title": "What is \"backprop\"?",
"_Tags": "<neural-networks><definitions><terminology>",
"_AnswerCount": "3",
"_CommentCount": "3"
},
(这是来自AI的json:stackexchange数据)
我请求有人帮我解决我的KeyError问题,因为我搜索过的其他来源无法帮助我
请提前谢谢你。
答案 0 :(得分:2)
首先,您必须访问"posts"
with open('C:/Documents/{}/Posts.json'.format(forum), encoding="utf8") as f:
j = json.load(f)
for row in j['posts']['row']:
parent_id = row['_Id']
body = format_data(row['_Body'])
# ...
答案 1 :(得分:1)
当您请求字典中不存在的密钥时,会引发KeyError。在你的情况下,从json开始,你似乎必须像这样访问它,
<强> json['posts' ]['row'][0].
强>
帖子是一个词典。 row是一个dicts列表。列表是有序的,这就是我们可以索引它的原因。
完整代码:
with open('C:/Documents/{}/Posts.json'.format(forum), encoding="utf8") as f:
jsondict = json.load(f)
# Remember, posts > row > first_index
row = jsondict['posts']['row'][0]
parent_id = row['_Id']
body = format_data(row['_Body'])
score = row['_Score']
comment_id = row['_Id']
comment_id_type = row['_PostTypeId']
parent_id_type = row['_PostTypeId']
accepted_answer_id = row['_AcceptedAnswerId']
accepted_parent_id = row['_ParentId']
...