[英]Python error: AttributeError: 'str' object has no attribute 'read'
[英]AttributeError: 'str' object has no attribute 'items' error
我正在编写一个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"
},......
我认为这是因为它将json文件解释为字符串。 我认为您可能希望看到它是否对您有帮助: 使用Python从JSON API提取数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.