TypeError: string indices must be integers Python 2

TypeError: string indices must be integers Python 2

我正在使用 python 2.7 并在

处遇到错误

print 'Article ID: ', paper['MedlineCitation']['PMID']

错误是 TypeError

Article ID: Traceback (most recent call last): File "F:/Practicals/Pubmed/pubmed-master/pubmed-master/scrap_pubmed.py", line 44, in print 'Article ID: ', paper['MedlineCitation']['PMID'] TypeError: string indices must be integers

if __name__ == '__main__':
    print "Enter keyword to search for: "
    keyword = raw_input()
    print "Enter number # of articles to fetch (number): "
    number_of_articles = raw_input()
    results = search(keyword, number_of_articles)
    id_list = results['IdList']
    papers = fetch_details(id_list)
    filename = 'pubmed.txt'
    target = open(filename, 'w')

    for i, paper in enumerate(papers):
        number_of_authors = 0
        print 'Article ID: ', paper['MedlineCitation']['PMID']
        target.write('Article ID: ' + paper['MedlineCitation']['PMID'] + '\n')

        print 'ISSN No: ', paper['MedlineCitation']['Article']['Journal']['ISSN']
        target.write('ISSN No: ' + paper['MedlineCitation'][
                     'Article']['Journal']['ISSN'] + '\n')

是你循环错了

您必须通过将 el 作为键来访问字典对象。

像这样:

  for i, paper in enumerate(papers):
    print papers[paper][0]['MedlineCitation']['PMID']