Type Error-String indices must be integers

huangapple go评论84阅读模式
英文:

Type Error-String indices must be integers

问题

您遇到了一个类型错误,指出字符串索引必须是整数。这通常发生在尝试以字符串索引字典时。

在您的代码中,data是一个字典,而您尝试在data['GetEventHeadlines_Response_1']['EventHeadlines']['Headline']上进行迭代,但它似乎包含更多的嵌套结构。您可能需要进一步迭代或查找正确的键以访问所需的值。

如果您需要更多帮助,请提供更多上下文或代码,我将尽力帮助您解决问题。

英文:

I am getting type error while iterating over the Dictionary. Type Error-String indices must be integers

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)

error:

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

答案1

得分: 2

将您的代码中的 false 替换为 False,它将正常工作。我已经使用了另一种方法,将其视为原始字符串,然后将其转换为 JSON。这样,JSON 的实际布尔值将转换为 Python 的方式。

import json

data = r'''{ ... }'''
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)

如果您需要进一步的帮助,请告诉我。

英文:

Convert your false with False and it will work.
I have used another method of doing it, which takes this as a raw string and then converting it to JSON. In this way, the actual boolean values of JSON will be converted to Python way.

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)

答案2

得分: 1

I've tried copying the code here and testing it and I didn't get the error.
我已经尝试复制代码并测试,没有出现错误。

I did edit a couple of values e.g. false to False but that's about it.
我确实编辑了一些值,例如将小写的false改成了大写的False,但就这样。

The result I got was as follows:
我得到的结果如下:

{'Q3 2019 Nio Inc Earnings Call': '8874A5B3-06BC-4863-9890-9F6775E1A219'}

Q3 2019 Nio Inc Earnings Call 8874A5B3-06BC-4863-9890-9F6775E1A219

Here is the code in case I have copied it incorrectly.
以下是代码,以防我复制有误。

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)
英文:

I've tried copying the code here and testing it and I didn't get the error.
I did edit a couple of values e.g. false to False but that's about it.
The result I got was as follows:

{'Q3 2019 Nio Inc Earnings Call': '8874A5B3-06BC-4863-9890-9F6775E1A219'}

Q3 2019 Nio Inc Earnings Call 8874A5B3-06BC-4863-9890-9F6775E1A219

Here is the code in case I have copied it incorrectly.

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)

huangapple
  • 本文由 发表于 2020年1月7日 00:35:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定