AttributeError:'str'对象没有属性'items'错误

时间:2017-10-04 14:13:34

标签: json xml python-3.x

我正在编写一个python脚本,用于从json文件中提取信息。我打印的书的标题是姓Marcus。我有输出,但它有错误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 个答案:

答案 0 :(得分:1)

我认为这是因为它将json文件解释为字符串。我想你可能希望看到它,如果它可以帮助你: Extract data from JSON API using Python