我有一个由json数据组成的链接,如下所示:
http://www.bom.gov.au/fwo/IDQ60801/IDQ60801.94182.json
来自链接的示例数据:
"header": [
{
"refresh_message": "Issued at 12:02 pm EST Thursday 1 March 2018",
"ID": "IDQ60801",
"main_ID": "IDQ60800",
"name": "Coconut Island",
"state_time_zone": "QLD",
"time_zone": "EST",
"product_name": "Weather Observations",
"state": "Queensland"
}
],
如何通过python将上述数据加载到postgres表中?我应该先将它加载到数据框中,然后再将其加载到postgres中吗?我是数据框架和熊猫的新手,所以我想寻求你的帮助。
我在postgres中的预期表格如下所示:
ID | main_id| name | state_timezone | time_zone | product_name | state
IDQ60801|IDQ60800|Coconut Island| QLD | EST |Weather Observations| Queensland
感谢您的提前帮助
答案 0 :(得分:0)
跳过数据帧和pandas。
使用Python的json
模块来解析数据。这将以字典的形式提供数据。
import json
import requests
json_response = requests.get('http://www.bom.gov.au/fwo/IDQ60801/IDQ60801.94182.json')
json_content = json.loads(json_response.content)
# navigate the json, which are nested lists of dicts
# this below gives you the first, and only, header-dict
header_dict = json_content['observations']['header'][0]
将您的数据放入字典中,您可以按照以下有关如何将其导入数据库的完美答案:Insert Python Dictionary using Psycopg2