2017-10-04 103 views
-1

我正在写一个python脚本来从json文件中提取信息。我将姓氏的名称打印为Marcus。我有输出,但它有错误AttributeError的:“海峡”对象有没有属性“项目”的错误以及AttributeError:'str'对象没有属性'items'错误

import json 
from pprint import pprint 
with open('bibliography.json.txt', encoding='utf-8') as data_file:  
data = json.load(data_file) 
for entry in data['bibliography']['biblioentry']: 
for authors in entry['author']: 
for key,val in authors.items(): 
if(key== 'lastname' and val=='Marcus'): 
title=entry['title'] 
print(title) 

JSON文件看起来是这样的:

{ 
    "bibliography": { 
    "biblioentry": [ 
     { 
     "-type": "Journal Article", 
     "title": "A brief survey of web data extraction tools", 
     "author": [ 
      { 
      "firstname": "Alberto", 
      "middlename": "HF", 
      "lastname": "Laender" 
      }, 
      { 
      "firstname": "Berthier", 
      "middlename": "A", 
      "lastname": "Ribeiro-Neto" 
      }, 
      { 
      "firstname": "Altigran", 
      "middlename": "S", 
      "lastname": "da Silva" 
      }, 
      { 
      "firstname": "Juliana", 
      "middlename": "S", 
      "lastname": "Teixeira" 
      } 
     ], 
     "details": { 
      "journalname": "ACM Sigmod Record", 
      "volume": "31", 
      "number": "2", 
      "pages": "84-93" 
     }, 
     "year": "2002", 
     "publisher": "ACM" 
     },...... 
+1

嗨,欢迎来到StackOverflow。请参考[stackoverflow.com/help/how-to-ask](http://stackoverflow.com/help/how-to-ask)了解如何根据指南提出正确的问题并改进您的问题。第一步可以正确格式化您的片段以提高可读性,这将有助于其他用户提供帮助。 – Mindsers

回答

相关问题