我试图读取一个Json文件,将其转换为csv 我看了一些问题,它帮助了我,但我仍然有一个小问题。 有些字段我不感兴趣,我不知道如何对脚本说跳过这些字段。你知道吗
这是我的密码:
import csv
import json
x = """{
"year": "2008",
"title": "Movies",
"items": [
{
"title": "The dark knight",
"description": "A batman story"
},
{
"title": "The Mummy: Tomb of the Dragon Emperor",
"description": "A mummy story"
},
{
"title": "Ironman",
"description": "An Avengers story"
}
]
}"""
x = json.loads(x)
for key, value in x.items():
print(key, value)
当然,我选择了一个非常简单的例子来说明我的例子。你知道吗
当我运行脚本时,我得到:
(u'items', [{u'description': u'A batman story', u'title': u'The dark knight'}, {u'description': u'A mummy story', u'title': u'The Mummy: Tomb of the Dragon Emperor'}, {u'description': u'An Avengers story', u'title': u'Ironman'}]) (u'title', u'Movies') (u'year', u'2008')
我想知道如何只显示项目的字段,并跳过字段“年”和“标题”在乞丐。你知道吗
因此,我想得到以下输出:
[{u'description': u'A batman story', u'title': u'The dark knight'}, {u'description': u'A mummy story', u'title': u'The Mummy: Tomb of the Dragon Emperor'}, {u'description': u'An Avengers story', u'title': u'Ironman'}])
我只需要访问这些字段,然后将它们写入csv文件。 你知道我该怎么做吗?你知道吗
事先非常感谢:)
你可以这样做:
json.loads
将JSON解析为dictionary。你知道吗您可以直接访问“items”键:
相关问题 更多 >
编程相关推荐