类型错误字符串索引必须为整数

时间:2020-01-06 16:35:27

标签: python json jupyter-notebook

我在字典上迭代时遇到类型错误。错误字符串类型索引必须为整数

data = {"GetEventHeadlines_Response_1":{"EventHeadlines":{"Headline":[{"CountryCode":"US","EventType":"EarningsCallsAndPresentations","Duration":{"EndDateTime":"2019-12-30T12:00:00","EndQualifier":"None","IsEstimate":false,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"EventId":12969284,"LastUpdate":"2020-01-02T19:06:09.267","LiveDialIn":{"Duration":{"EndDateTime":"2019-12-30T12:00:00","EndQualifier":"None","IsEstimate":false,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"Password":"6378226","PhoneNumber":"+1-845-675-0437","Status":"Available"},"LiveWebcast":{"Type":"ThirdParty","Duration":{"EndDateTime":"2019-12-30T14:00:00","EndQualifier":"DateTime","IsEstimate":false,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"WebcastId":"840B9947-C69D-40E5-AF4F-4F35B1FF069C","Provider":"WindowsMedia","Url":null},"Name":"Q3 2019 Nio Inc Earnings Call","ReplayDialIn":{"Duration":{"EndDateTime":"2020-01-07T12:59:00","EndQualifier":"DateTime","IsEstimate":false,"StartDateTime":"2019-12-30T15:00:00","StartQualifier":"DateTime"},"Password":"6378226","PhoneNumber":"+1-646-254-3697","Status":"Available"},"ReplayWebcast":{"Type":"ThirdParty","Duration":{"EndDateTime":"2020-01-30T12:00:00","EndQualifier":"DateOnly","IsEstimate":false,"StartDateTime":"2019-12-30T14:00:00","StartQualifier":"DateTime"},"WebcastId":"0816103B-4559-4816-BE2A-BC1B5612486B","Provider":"WindowsMedia","Url":null},"Transcript":{"Status":"Final","TranscriptId":"8874A5B3-06BC-4863-9890-9F6775E1A219","DeliveryType":"Live","Locale":"en-US"},"Organization":{"Name":"Nio Inc","Symbols":{"Symbol":[{"Type":"RIC","Value":"NIO.N"}]}},"RsvpRequired":false}]},"PaginationResult":{"PageNumber":1,"RecordsOnPage":1,"RecordsPerPage":100,"TotalRecords":1}}}

Doc_details= dict()

 for item in data['GetEventHeadlines_Response_1']['EventHeadlines']['Headline']:
 name= item['Name']
 docid=item['Transcript']['TranscriptId']
 Doc_details[name]=docid
print(Doc_details)

for key, value in Doc_details.items():
 print(key,value)

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-165-4ad75716058a> in <module>()
  1 Doc_details= {}
  2 
----> 3 for item in data['GetEventHeadlines_Response_1']['EventHeadlines']['Headline']:
  4     name= item['Name']
  5     docid=item['Transcript']['TranscriptId']

TypeError: string indices must be integers

2 个答案:

答案 0 :(得分:2)

将您的false转换为False,它将起作用。 我使用了另一种方法,将其作为原始字符串,然后将其转换为JSON。这样,JSON的实际布尔值将转换为Python方式。

import json
data = r'''{"GetEventHeadlines_Response_1":{"EventHeadlines":{"Headline":[{"CountryCode":"US","EventType":"EarningsCallsAndPresentations","Duration":{"EndDateTime":"2019-12-30T12:00:00","EndQualifier":"None","IsEstimate":false,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"EventId":12969284,"LastUpdate":"2020-01-02T19:06:09.267","LiveDialIn":{"Duration":{"EndDateTime":"2019-12-30T12:00:00","EndQualifier":"None","IsEstimate":false,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"Password":"6378226","PhoneNumber":"+1-845-675-0437","Status":"Available"},"LiveWebcast":{"Type":"ThirdParty","Duration":{"EndDateTime":"2019-12-30T14:00:00","EndQualifier":"DateTime","IsEstimate":false,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"WebcastId":"840B9947-C69D-40E5-AF4F-4F35B1FF069C","Provider":"WindowsMedia","Url":null},"Name":"Q3 2019 Nio Inc Earnings Call","ReplayDialIn":{"Duration":{"EndDateTime":"2020-01-07T12:59:00","EndQualifier":"DateTime","IsEstimate":false,"StartDateTime":"2019-12-30T15:00:00","StartQualifier":"DateTime"},"Password":"6378226","PhoneNumber":"+1-646-254-3697","Status":"Available"},"ReplayWebcast":{"Type":"ThirdParty","Duration":{"EndDateTime":"2020-01-30T12:00:00","EndQualifier":"DateOnly","IsEstimate":false,"StartDateTime":"2019-12-30T14:00:00","StartQualifier":"DateTime"},"WebcastId":"0816103B-4559-4816-BE2A-BC1B5612486B","Provider":"WindowsMedia","Url":null},"Transcript":{"Status":"Final","TranscriptId":"8874A5B3-06BC-4863-9890-9F6775E1A219","DeliveryType":"Live","Locale":"en-US"},"Organization":{"Name":"Nio Inc","Symbols":{"Symbol":[{"Type":"RIC","Value":"NIO.N"}]}},"RsvpRequired":false}]},"PaginationResult":{"PageNumber":1,"RecordsOnPage":1,"RecordsPerPage":100,"TotalRecords":1}}}'''
data = json.loads(data)

Doc_details= dict()

for item in data['GetEventHeadlines_Response_1']['EventHeadlines']['Headline']:
    name= item['Name']
    docid=item['Transcript']['TranscriptId']
    Doc_details[name]=docid

print(Doc_details)

for key, value in Doc_details.items():
    print(key,value)

答案 1 :(得分:1)

我尝试在此处复制代码并对其进行测试,但未收到错误。 我确实编辑了几个值,例如假到假,仅此而已。 我得到的结果如下:

{'Q3 2019 Nio Inc收入电话:'8874A5B3-06BC-4863-9890-9F6775E1A219'}

2019年第三季度Nio Inc收入电话8874A5B3-06BC-4863-9890-9F6775E1A219

如果我没有正确地复制它,这是代码。

data = {"GetEventHeadlines_Response_1":{"EventHeadlines":{"Headline":[{"CountryCode":"US","EventType":"EarningsCallsAndPresentations","Duration":{"EndDateTime":"2019-12-30T12:00:00","EndQualifier":"None","IsEstimate":False,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"EventId":12969284,"LastUpdate":"2020-01-02T19:06:09.267","LiveDialIn":{"Duration":{"EndDateTime":"2019-12-30T12:00:00","EndQualifier":"None","IsEstimate":False,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"Password":"6378226","PhoneNumber":"+1-845-675-0437","Status":"Available"},"LiveWebcast":{"Type":"ThirdParty","Duration":{"EndDateTime":"2019-12-30T14:00:00","EndQualifier":"DateTime","IsEstimate":False,"StartDateTime":"2019-12-30T12:00:00","StartQualifier":"DateTime"},"WebcastId":"840B9947-C69D-40E5-AF4F-4F35B1FF069C","Provider":"WindowsMedia"},"Name":"Q3 2019 Nio Inc Earnings Call","ReplayDialIn":{"Duration":{"EndDateTime":"2020-01-07T12:59:00","EndQualifier":"DateTime","IsEstimate":False,"StartDateTime":"2019-12-30T15:00:00","StartQualifier":"DateTime"},"Password":"6378226","PhoneNumber":"+1-646-254-3697","Status":"Available"},"ReplayWebcast":{"Type":"ThirdParty","Duration":{"EndDateTime":"2020-01-30T12:00:00","EndQualifier":"DateOnly","IsEstimate":False,"StartDateTime":"2019-12-30T14:00:00","StartQualifier":"DateTime"},"WebcastId":"0816103B-4559-4816-BE2A-BC1B5612486B","Provider":"WindowsMedia"},"Transcript":{"Status":"Final","TranscriptId":"8874A5B3-06BC-4863-9890-9F6775E1A219","DeliveryType":"Live","Locale":"en-US"},"Organization":{"Name":"Nio Inc","Symbols":{"Symbol":[{"Type":"RIC","Value":"NIO.N"}]}},"RsvpRequired":False}]},"PaginationResult":{"PageNumber":1,"RecordsOnPage":1,"RecordsPerPage":100,"TotalRecords":1}}} 

Doc_details= {}

for item in data['GetEventHeadlines_Response_1']['EventHeadlines']['Headline']:
    name= item['Name']
    docid=item['Transcript']['TranscriptId']
    Doc_details[name]=docid
print(Doc_details)

for key, value in Doc_details.items():
    print(key,value)