博客
关于我
python作业之JSON数据的处理
阅读量:276 次
发布时间:2019-03-01

本文共 3063 字,大约阅读时间需要 10 分钟。

Python与JSON数据处理指南

在处理JSON数据时,Python提供了强大的json模块,能够方便地进行数据的序列化与反序列化。以下是关于JSON与Python数据类型对应关系的详细说明,以及如何将给定的JSON数据转换为Python数据结构。

JSON与Python数据类型对应关系

  • JSON数据类型 | Python数据类型
  • object | dict
  • array | list
  • number | int/float
  • true/false/null | Python布尔值/None

数据处理步骤

import json# 将JSON数据转换为Python字典data_iphone_json = '''{    "data": [        {            "creator": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "updater": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "deleter": null,            "createTime": "2020-03-26T02:41:06.491Z",            "updateTime": "2020-03-26T02:46:27.825Z",            "deleteTime": null,            "_widget_1557886562320": "iPhone 11",            "_widget_1557886562335": "5998",            "_widget_1557886562350": "17",            "_id": "5e7c164229e01a00063be284",            "appId": "5e798363b587cc0006b40445",            "entryId": "5cdb765b5a6ae613aeed0f84"        },        {            "creator": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "updater": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "deleter": null,            "createTime": "2020-03-26T02:47:02.037Z",            "updateTime": "2020-03-26T02:47:02.037Z",            "deleteTime": null,            "_widget_1557886562320": "iPhone X",            "_widget_1557886562335": "4998",            "_widget_1557886562350": "5",            "_id": "5e7c17a650bccb0006441778",            "appId": "5e798363b587cc0006b40445",            "entryId": "5cdb765b5a6ae613aeed0f84"        },        {            "creator": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "updater": {                "_id": "5cecd40dd23e194ab0867aab",                "name": "查理",                "username": "cxt7777"            },            "deleter": null,            "createTime": "2020-03-26T02:47:43.059Z",            "updateTime": "2020-03-26T02:47:43.059Z",            "deleteTime": null,            "_widget_1557886562320": "iPhone 8",            "_widget_1557886562335": "3998",            "_widget_1557886562350": "32",            "_id": "5e7c17cfcd87510006cf8189",            "appId": "5e798363b587cc0006b40445",            "entryId": "5cdb765b5a6ae613aeed0f84"        }    ]}'''# 转换JSON为Python数据data_iphone = json.loads(data_iphone_json)# 提取iPhone 8的商品数量num_iphone8 = data_iphone['data'][2]['_widget_1557886562350']print(num_iphone8)# 提取所有商品价格def get_price(data_iphone_products, product_key):    product_price = []    for products in data_iphone_products['data']:        product_price.append(products[product_key])    return product_priceprice = get_price(data_iphone, '_widget_1557886562335')print(price)

转载地址:http://wldo.baihongyu.com/

你可能感兴趣的文章
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>