使用python读取JSON:keyrerror

使用python读取JSON:keyrerror,python,json,Python,Json,对于创建数据库的项目,我想使用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']

对于创建数据库的项目,我想使用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'
文件“C:\Python\database.py”,第85行,在
父项id=行[''id']
KeyError:“\u Id”
我已经阅读了这个错误发现,根据官方python文档,exception KeyError是

在现有键集中找不到映射(字典)键时引发。 现在我很难理解这个语法,因为json文件中存在“\u Id”(如下所示)

{
“员额”:{
“行”:[
{
“_Id”:“1”,
“_PostTypeId”:“1”,
“_AcceptedAnswerId”:“3”,
“_CreationDate”:“2016-08-02T15:39:14.947”,
“_分数”:“5”,
“_ViewCount”:“254”,
“\u Body”:“backprop”是什么意思?我在谷歌上搜索过,但它显示了反向传播。

\n\n这个“backprop”术语与“backpropagation”基本相同还是有不同的含义?

\n”, “_OwnerUserId”:“8”, “_LastEditorUserId”:“7488”, “_lasteddate”:“2017-05-28813:48:02.003”, “_LastActivityDate”:“2017-05-28813:48:02.003”, “_Title”:“backprop”是什么?”, “_标签”:”, “_AnswerCount”:“3”, _CommentCount:“3” },
(这是来自AI:stackexchange数据的json)

我请求某人帮助我解决我的关键错误,因为我搜索的其他来源没有给我任何帮助


请提前向您表示感谢。

首先您必须访问
“posts”


首先,您必须访问
“posts”


当您请求字典中不存在的密钥时,会引发KeyError。在您的情况下,从json看来,您必须像这样访问它

json['posts']['row'][0]。

posts是一个dict。row是一个dict列表。列表是有序的,这就是为什么我们可以索引到其中

完整代码:

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']
            ...

当您请求字典中不存在的密钥时,会引发KeyError。在您的情况下,从json看来,您必须像这样访问它

json['posts']['row'][0]。

posts是一个dict。row是一个dict列表。列表是有序的,这就是为什么我们可以索引到其中

完整代码:

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']
            ...

您应该只使用Pandas读取JSON,并将数据帧转储到数据库…您应该只使用Pandas读取JSON,并将数据帧转储到数据库。。。
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']
            ...