【问题标题】:Parsing large JSON - Python [closed]解析大型 JSON - Python [关闭]
【发布时间】:2014-12-06 17:00:11
【问题描述】:

我正在使用Public API,它列出了 coursera 中的所有诅咒。 json 大约 8MB。现在,我的问题是如何有效地解析 json 并将内容保存在我的数据库中。

这是我的代码:-

import requests
r = requests.get('https://www.coursera.org/maestro/api/topic/list2') # Is getting all the information at once, the standard way of doing it or should I get in chunks?
print r.json()

# Now to save the details, should I use a NoSQL DB.
# I've little experience of using a NoSQL DB, hence for building an app that list all coursera courses, 
# saving data inside a Mongo will be a good choice or not. 

谢谢

【问题讨论】:

  • 这个问题太宽泛了。您已经对其进行了解析,并且有上千种方法可以保存它。
  • 做一些研究:了解不同类型的 NoSQL 数据库,将它们的功能与您的应用所需的功能进行比较,选择一个并学习如何将其与 Python 一起使用。

标签: python json


【解决方案1】:

我不想卷入 SQL 与 NoSQL 的争论。另外,我对你的项目没有足够的了解,无法给你建议。但您似乎对 SQL 有一定的经验,并且您希望:

  • 探索 json 以了解您可以在应用中使用什么
  • 找出合适的数据库架构
  • 解析 json 并将它们插入到您刚刚创建的数据库中。

我没有搜索过,但也许 coursera 有关于这个 http 请求提供的信息的文档。您可以使用它来指导您的模型开发。

如果不是,或者如果您倾向于跳入数据并凭经验找出模型,好消息是 requests.json() 会自动将 json 内容解码为字典。

要探索这个字典,你可以使用 dict.keys() 方法

>>> r.json().keys() # returns the following line:
dict_keys(['unis', 'insts', 'cats', 'topics', 'courses'])

递归执行此操作以了解每个节点下的内容。如果您点击列表,请检查其中的几个列表。这些列表可能会转换为 sql 世界中的行。如果列表包含字典,那么它会让您了解字段名称是什么。如果进一步,这个列表中的字典有嵌套的字典,这可能表明关系

例如,

>>> r.json()['unis'].keys() # gives me the following error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'keys'

所以,我试过了,

>>> pp.pprint(r.json()['unis'][0]) # which gave me the first record
{'abbr_name': 'Stanford',
 'banner': 'https://coursera-university-assets.s3.amazonaws.com/73/a47990ea7c11e3b00589d092602f0d/Stanford-University-Banner-LRG.jpg',
 'class_logo': 'https://coursera-university-assets.s3.amazonaws.com/21/9a0294e2bf773901afbfcb5ef47d97/Stanford_Coursera-200x48_RedText_BG.png',
 'description': 'The Leland Stanford Junior University, commonly referred to '
                'as Stanford University or Stanford, is an American private '
                'research university located in Stanford, California on an '
                '8,180-acre (3,310 ha) campus near Palo Alto, California, '
                'United States.',
 'display': True,
 'favicon': 'https://coursera-university-assets.s3.amazonaws.com/dc/581cda352d067023dcdcc0d9efd36e/favicon-stanford.ico',
 'home_link': 'http://online.stanford.edu/',
 'id': 1,
 'landing_page_banner': 'https://coursera-university-assets.s3.amazonaws.com/6f/75dd30dd5911e38988193a0e8ad8fe/Stanford_Coursera-200x48_RedText_BG.jpg',
 'location': 'Palo Alto, CA, United States',
 'location_city': 'Palo Alto',
 'location_country': 'US',
 'location_lat': 37.4418834,
 'location_lng': -122.14301949999998,
 'location_state': 'CA',
 'logo': 'https://coursera-university-assets.s3.amazonaws.com/d8/4c69670e0826e42c6cd80b4a02b9a2/stanford.png',
 'name': 'Stanford University',
 'partner_type': 1,
 'primary_color': '#8C1515',
 'rectangular_logo_svg': 'https://coursera-university-assets.s3.amazonaws.com/d6/cb68d0d09b11e3a575e17d6a22968b/SUSig_StnfrdOnly.svg',
 'short_name': 'stanford',
 'square_logo': 'https://coursera-university-assets.s3.amazonaws.com/e3/cebbb0d0a311e39b31794df7e5d956/Coursera-SUSig_StnfrdUStack_SQ.png',
 'square_logo_source': 'https://coursera-university-assets.s3.amazonaws.com/e2/c49eb0d0a311e3ad37254033038522/Coursera-SUSig_StnfrdUStack_SQ.png',
 'square_logo_svg': 'https://coursera-university-assets.s3.amazonaws.com/e0/0dbc10d0a311e3ad37254033038522/Coursera-SUSig_StnfrdUStack_SQ.svg',
 'website': '',
 'website_facebook': '',
 'website_twitter': '',
 'website_youtube': ''}

从现在开始,我会天真地创建一个名为 coursera_unis 的表,其中包含这行代码返回的以下字段:

>>> r.json()['unis'][0].keys()
dict_keys(['website_facebook', 'location', 'website_twitter', 'square_logo', 'favicon', 'id', 'website', 'location_lng', 'logo', 'location_lat', 'partner_type', 'short_name', 'website_youtube', 'square_logo_svg', 'banner', 'primary_color', 'location_country', 'rectangular_logo_svg', 'square_logo_source', 'name', 'landing_page_banner', 'display', 'home_link', 'description', 'abbr_name', 'location_city', 'location_state', 'class_logo'])

然后,下一步是插入数据。 It's already answered in this SO thread for MySQL。其他数据库后端也存在类似的选项,所以应该不会太难。

【讨论】:

  • 感谢 Haleemur 的深入回复。由于两个原因,我正在考虑使用 NoSQL DB。 1)一个对象中有太多信息,可能有用也可能没用,因此我正在考虑使用 NoSQL DB,因为它可以通过创建一行和一列来存储信息。 2) 我在 NoSQL 方面的经验很少,在某种程度上我会掌握同样的知识。
  • 我想听听您对此的看法。我的最终目标是利用 coursera 的公共 API 制作一个具有列表视图和详细视图的 Web 应用程序。参考网址:-coursera.org/courses
  • 看起来是一个足够简单的应用程序,不需要太多的持久层。如果您选择合适的 NoSQL 存储或 RDBMS 存储,它会很好地工作。设置 RDBMS 可能需要做更多的工作,但您仍然可以在一个下午启动并运行。如果你想用它来学习你不太熟悉的东西,那就去吧!
猜你喜欢
  • 2018-08-29
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 2015-08-19
相关资源
最近更新 更多