2.3 Python数据处理案例讲解

本节主题: 2.3 Python数据处理案例讲解

课程讲师: Yunlin

观看地址: 点我进入


1 本节要点

  • 上期作业讲解
  • 简道云数据处理演示

2 课前准备


3 课程内容

3.1 上期作业讲解

(演示)


3.2 简道云数据处理演示

a = {
    "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"
        }
    ]
}

数据结构解析

  • 将数据赋值给变量data_iphone后


变量名 data_iphone
数据类型 字典
key数据类型 字符串
key ‘data’
通过key获取value data_iphone[‘data’]
value数据类型 列表
value [{‘creator’: {’_id’: ‘5cecd40dd23e194ab0867aab’, ‘name’: ‘查理’, ‘username’: ‘cxt7777’}, ‘updater’: {’_id’: ‘5cecd40dd23e194ab0867aab’, ‘name’: ‘查理’, ‘username’: ‘cxt7777’}, ‘deleter’: None, ‘createTime’: ‘2020-03-26T02:41:06.491Z’, ‘updateTime’: ‘2020-03-26T02:46:27.825Z’, ‘deleteTime’: None, ‘_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’: None, ‘createTime’: ‘2020-03-26T02:47:02.037Z’, ‘updateTime’: ‘2020-03-26T02:47:02.037Z’, ‘deleteTime’: None, ‘_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’: None, ‘createTime’: ‘2020-03-26T02:47:43.059Z’, ‘updateTime’: ‘2020-03-26T02:47:43.059Z’, ‘deleteTime’: None, ‘_widget_1557886562320’: ‘iPhone 8’, ‘_widget_1557886562335’: ‘3998’, ‘_widget_1557886562350’: ‘32’, ‘_id’: ‘5e7c17cfcd87510006cf8189’, ‘appId’: ‘5e798363b587cc0006b40445’, ‘entryId’: ‘5cdb765b5a6ae613aeed0f84’}]

  • data_iphone的value解析


变量名 data_iphone[‘data’]
数据类型 列表
获取列表中第一个数据 data_iphone[‘data’][0]
列表中第一个数据的值 {‘creator’: {’_id’: ‘5cecd40dd23e194ab0867aab’, ‘name’: ‘查理’, ‘username’: ‘cxt7777’}, ‘updater’: {’_id’: ‘5cecd40dd23e194ab0867aab’, ‘name’: ‘查理’, ‘username’: ‘cxt7777’}, ‘deleter’: None, ‘createTime’: ‘2020-03-26T02:41:06.491Z’, ‘updateTime’: ‘2020-03-26T02:46:27.825Z’, ‘deleteTime’: None, ‘_widget_1557886562320’: ‘iPhone 11’, ‘_widget_1557886562335’: ‘5998’, ‘_widget_1557886562350’: ‘17’, ‘_id’: ‘5e7c164229e01a00063be284’, ‘appId’: ‘5e798363b587cc0006b40445’, ‘entryId’: ‘5cdb765b5a6ae613aeed0f84’}
列表中第一个数据的数据类型 字典
列表中第一个数据中的key为’_widget_1557886562320’的value data_iphone[‘data’][0][’_widget_1557886562320’]

4 课后作业

4.1 选择题作业

作业课次:2

注册课程后,方可答题:点击注册课程

选择题地址:点击查看


4.2 编程题作业

假如你已经使用接口测试工具调用简道云的查询多条数据接口,成功获取了数据,你下面需要使用Python进行如下的操作:

{
    "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"
        }
    ]
}
  • 将数据复制到Python,并赋值给变量data_iphone
    • 提示:需要先手动将json数据处理成Python的字典数据
  • 使用Python代码从数据中提取出iPhone 8的商品数量("_widget_1557886562350"),并赋值给变量num_iphone8
    • 提示:iphone 8是’data’里面的第3个数据,需要使用data_iphone[‘data’][2]进行索引
  • 使用for循环,从数据中提取出所有的商品价格("_widget_1557886562335"),并赋值给变量price(建议price使用列表格式)
  • (可选)尝试下自己调用简道云API接口,并用Python去处理数据
本文是否对您有帮助?
 有帮助
 没帮助
您是否遇到了以下问题?
如需获取即时帮助,请联系技术支持