-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"
},......
嗨,歡迎來到StackOverflow。請參考[stackoverflow.com/help/how-to-ask](http://stackoverflow.com/help/how-to-ask)瞭解如何根據指南提出正確的問題並改進您的問題。第一步可以正確格式化您的片段以提高可讀性,這將有助於其他用戶提供幫助。 – Mindsers