在python中,Nonetype并没有属性get item,但可以在源页面中看到值,并在相同的格式下调用
所以我的代码给了我一个错误:TypeError:'NoneType'对象没有我知道以前已经处理过的属性'getitem'。然而,我以前在同一个循环中做过两次相同的操作,没有任何问题,而且肯定有一些时候同义词pull没有答案。然而,这个定义应该总是返回一个答案。由于源代码的格式与在一行上运行的格式相同,但在计算机上应该是相同的,因此我无法忘记错误发生的原因。下面是我正在使用的代码和源代码的一个片段。谢谢 资料来源:在python中,Nonetype并没有属性get item,但可以在源页面中看到值,并在相同的格式下调用,python,json,attributes,format,nonetype,Python,Json,Attributes,Format,Nonetype,所以我的代码给了我一个错误:TypeError:'NoneType'对象没有我知道以前已经处理过的属性'getitem'。然而,我以前在同一个循环中做过两次相同的操作,没有任何问题,而且肯定有一些时候同义词pull没有答案。然而,这个定义应该总是返回一个答案。由于源代码的格式与在一行上运行的格式相同,但在计算机上应该是相同的,因此我无法忘记错误发生的原因。下面是我正在使用的代码和源代码的一个片段。谢谢 资料来源: links: {
links: {
nextPage: http://data.bioontology.org/ontologies/ICO/classes?page=2,
prevPage": null
},
-collection: [
-{
prefLabel: "document act",
synonym: [ ],
-definition: [
"A social act creating, revoking or transferring a socio-legal generically dependent continuant or a role by validating (signing, stamping, publishing) a document."
],
Python程序
import urllib2
import json
REST_URL = "http://data.bioontology.org"
API_KEY = "0640b94d-63f7-49f3-9be5-f79354797620"
def get_json(url):
opener = urllib2.build_opener()
opener.addheaders = [('Authorization', 'apikey token=' + API_KEY)]
return json.loads(opener.open(url).read())
# Get all ontologies from the REST service and parse the JSON
ontologies = get_json(REST_URL+"/ontologies")
# Iterate looking for ontology with acronym BRO
ont = None
for ontology in ontologies:
if ontology["acronym"] == "ICO":
ont = ontology
labels = []
synonyms=[]
definitions=[]
# Using the hypermedia link called `classes`, get the first page
page = get_json(ont["links"]["classes"])
# Iterate over the available pages adding labels from all classes
# When we hit the last page, the while loop will exit
next_page = page
while next_page:
next_page = page["links"]["nextPage"]
for ont_class in page["collection"]:
labels.append(ont_class["prefLabel"])
synonyms.append(ont_class["synonym"])
#if bro_class["definition"] is not None:
definitions.append(ont_class)["definition"]
if next_page:
page = get_json(next_page)
# Output the labels
#for label in labels:
# print label
#for synonym in synonyms:
# print synonym
#for definition in definitions:
# print definition
回溯:
回溯最近一次呼叫上次:
文件C:/Users/Godjira/PycharmProjects/NCBO-REST/get_all.py,第38行,在
定义.附录类[定义] TypeError:“非类型”对象没有属性“getitem”它可能是
definitions.append(ont_class["definition"])
哪一行触发异常?你能把回溯放在上面的问题中吗?因为您的代码没有为任何对象显示显式的.getItem方法。definitions.appendont_类[definition]会触发错误。这是回溯:回溯最近的调用最后一次:文件C:/Users/Godjira/PycharmProjects/NCBO-REST/get_all.py,第38行,在定义中!。。。。我绝对不能相信我错过了。这是其中之一,我已经看了太多,需要新鲜的眼睛。非常感谢。成功了!TypeError之后:“NoneType”对象没有属性,好问题是哪个函数调用返回None?在您的例子中是definitions.append,因为list.append返回None。因此找到了bug。