【问题标题】:Extract some data with python from a json content [closed]用python从json内容中提取一些数据[关闭]
【发布时间】:2020-05-05 19:16:49
【问题描述】:

大家好,

我有以下代码:

import json
import pprint

with open('77.json', 'r') as file:
data = json.load(file)

somedata = data.get('"name":')
printer = pprint.PrettyPrinter()

print('somedata')

json文件中的代码是这样的:

[ { "id": "0f78f3f6-0efe-49bc-9283-7daf23c142f0", "name": "利用网络设备进行 DHCP", “蛞蝓”:“利用-网络设备-for-dhcp”, “duration_in_seconds”:473, “状态”:“准备就绪”, “元数据”:{ “公司”:“xxx” }, “instructor_id”:“e72b1785-711e-42b4-b795-f3523fe3a7d8”, “创建”:“2020-04-15T01:18:51Z”, “修改”:“2020-04-30T18:34:46.111771Z”, "s3_video_url": "http//:blabla4", “excel_id”:“e1ZTXDBr” }, {

问题是我的代码出错了:

 python test.py
 Traceback (most recent call last):
 File "test.py", line 7, in <module>
somedata = data.get('"name":')
AttributeError: 'list' object has no attribute 'get'

我想从“名称”中提取所有内容:

这是 json 内容的外观: https://video.rmotr.com/api/v1/videos?page_size=none&ids=5114e573,a2ad2115,e2695b5e,f0b3f815,27f349a9,dd1e51f4,1f9aba83,d09dae17,2d20f05a,6a90013d,628d73da,512eb96f,41677d9e,0a816753,0fbac17e,198911c1,0f78f3f6,6d300633,1a8072ca,d0853666,a6215f7a,8247903e,cd0f87a3,0afe6c8e,c65927d7,b26d523e,c1c45d2d,82ccc733,cf6b2949,220696da,2d38eb42,5fc5b96f,f84ff818,8037600d,33eaa6ee,9d319c7a,2889e3e0,d511906c,d13c6b4d,ac8e0c0c,c0ddcf1f,5402d2bc,9355a08c,438db984,abe48951,f4bafb9e,54418fb3,bfa32e61,264be932,549340c7,fe1ebace,c4eed340,1f1747dc,bae9ee4b,1f5a8478,11bec186,eb57362d,68ff2689,6f60b2f7,3d154afd,40706ef8,5b52955e,4607b4e6,c32fa88f,46290537,b8755d8e,cd61a627,0cfc4e27,88804d99,d53474ea&parent_type=course&parent_id=9581c375-e70d-4e54-a425-51a05c02157a

我想从以“名称”开头的每一行中提取所有信息:

应该是这样的:

  Introducing Northbound & Southbound APIs
  SDN’s Relationship To Management, Control & Data Planes

以此类推。

对不起,我忘了说我是初学者。

提前致谢

【问题讨论】:

  • 异常AttributeError: 'list' object has no attribute 'get'表示数据是list,而list没有get属性。
  • 数据[“名称”]呢?

标签: python json api


【解决方案1】:

您的 json 文件包含一个对象数组。所以当你加载它时,你会得到一个字典列表。要获取名称字段,您需要查看列表中的各个条目:

for entry in data:
    somedata = entry.get('name')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    相关资源
    最近更新 更多