首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将单个项目列表转换为字典?

如何将单个项目列表转换为字典?
EN

Stack Overflow用户
提问于 2022-07-21 21:14:25
回答 2查看 51关注 0票数 0
代码语言:javascript
复制
symbol = 'AAPL'
api_url = f'https://financialmodelingprep.com/api/v3/profile/{symbol}?apikey={FMP_API_TOKEN}'
data = requests.get(api_url).json()
data

我在一个木星笔记本中调用了这个api,我收到的json是一个项目列表,如下所示:

代码语言:javascript
复制
[{'symbol': 'AAPL',
  'price': 155.35,
  'beta': 1.19455,
  'volAvg': 93789221,
  'mktCap': 2514370953216,
  'lastDiv': 0.89,
  'range': '129.04-182.94',
  'changes': 2.3100128,
  'companyName': 'Apple Inc.',
  'currency': 'USD',
  'cik': '0000320193',
  'isin': 'US0378331005',
  'cusip': '037833100',
  'exchange': 'NASDAQ Global Select',
  'exchangeShortName': 'NASDAQ',
  'industry': 'Consumer Electronics',
  'website': 'https://www.apple.com',
  'description': 'Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. It also sells various related services. In addition, the company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; AirPods Max, an over-ear wireless headphone; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, HomePod, and iPod touch. Further, it provides AppleCare support services; cloud services store services; and operates various platforms, including the App Store that allow customers to discover and download applications and digital content, such as books, music, video, games, and podcasts. Additionally, the company offers various services, such as Apple Arcade, a game subscription service; Apple Music, which offers users a curated listening experience with on-demand radio stations; Apple News+, a subscription news and magazine service; Apple TV+, which offers exclusive original content; Apple Card, a co-branded credit card; and Apple Pay, a cashless payment service, as well as licenses its intellectual property. The company serves consumers, and small and mid-sized businesses; and the education, enterprise, and government markets. It distributes third-party applications for its products through the App Store. The company also sells its products through its retail and online stores, and direct sales force; and third-party cellular network carriers, wholesalers, retailers, and resellers. Apple Inc. was incorporated in 1977 and is headquartered in Cupertino, California.',
  'ceo': 'Mr. Timothy Cook',
  'sector': 'Technology',
  'country': 'US',
  'fullTimeEmployees': '154000',
  'phone': '14089961010',
  'address': '1 Apple Park Way',
  'city': 'Cupertino',
  'state': 'CALIFORNIA',
  'zip': '95014',
  'dcfDiff': 2.07176,
  'dcf': 155.112,
  'image': 'https://financialmodelingprep.com/image-stock/AAPL.png',
  'ipoDate': '1980-12-12',
  'defaultImage': False,
  'isEtf': False,
  'isActivelyTrading': True,
  'isAdr': False,
  'isFund': False}] 

我知道,如果我收到的json是字典而不是列表,那么完成我的项目就更容易了。如何将此列表转换为字典,或者每次调用api时只打印一个字典?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-21 21:18:45

如果它总是一个条目列表,那么只需调用列表中的第一个元素就可以得到字典。

代码语言:javascript
复制
data = requests.get(api_url).json()[0]
票数 1
EN

Stack Overflow用户

发布于 2022-07-21 21:25:26

处理此问题的健壮方法是创建一个处理单个字典的函数:

代码语言:javascript
复制
def response_handler(data: dict):
    # Do what you need to do

然后,当您得到列表响应时,只需调用每个字典上的处理程序:

代码语言:javascript
复制
for data in response:
    response_handler(data)

也就是说,如果您只需要第一个元素,根据其他响应,那么只需要requests.get(api_url).json()[0]来获取第一个元素。请注意,您正在获得一个字典列表--而且您只对字典感兴趣。因此,如果该列表的第一个元素是您所关心的字典,则只需对您所拥有的列表( [0]下标)进行索引。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73072700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档