[英]How to resolve -'NoneType' object is not subscriptable
我正在從 API 中獲取一些數據並將響應 JSON 的內容保存到列表中。 但是我收到“‘NoneType’object 不可訂閱”錯誤。 我知道我正在索引 none object。我該如何解決? 我對 Python 很陌生。
for i in range(0,len(content)):
try:
response = requests.post(url, data=json.dumps({
"GetSignificantDevelopments_Request_1": {
"FindRequest": {
"CompanyIdentifiers_typehint": [
"CompanyIdentifiers"
],
"CompanyIdentifiers": [
{
"RIC": {
#"Value": content[i]
"Value": "8341.T"
}
}
],
"StartDate": "2020-08-01T00:00:00",
"EndDate": "2020-09-21T00:00:00",
"Significance": "1 2 3",
"MaxNumberOfItems": 2000
}
}
}), headers=headers)
data=json.loads(response.text.encode('utf8'))
for item in data['GetSignificantDevelopments_Response_1']['FindResponse']['Development']:
list_RepNo=[]
list_DevelopmenId=[]
list_RepNo.append(item['Xrefs']['RepNo'])
list_DevelopmenId.append(item['Xrefs']['DevelopmentId'])
except Exception as Error:
print(Error)
raise
continue
我得到的錯誤如下:
'NoneType' object is not subscriptable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-c73ee1d65472> in <module>
36 }), headers=headers)
37 data=json.loads(response.text.encode('utf8'))
---> 38 for item in data['GetSignificantDevelopments_Response_1']['FindResponse']['Development']:
39 list_RepNo=[]
40 list_DevelopmenId=[]
TypeError: 'NoneType' object is not subscriptable
我該如何解決這個問題?
我正在從API中獲取一些數據,並將響應JSON的內容保存到列表中。 但是我收到“'NoneType'對象不可下標”錯誤。 我了解我正在索引一個none對象。 我該如何解決? 我對Python很陌生。
for i in range(0,len(content)):
try:
response = requests.post(url, data=json.dumps({
"GetSignificantDevelopments_Request_1": {
"FindRequest": {
"CompanyIdentifiers_typehint": [
"CompanyIdentifiers"
],
"CompanyIdentifiers": [
{
"RIC": {
#"Value": content[i]
"Value": "8341.T"
}
}
],
"StartDate": "2020-08-01T00:00:00",
"EndDate": "2020-09-21T00:00:00",
"Significance": "1 2 3",
"MaxNumberOfItems": 2000
}
}
}), headers=headers)
data=json.loads(response.text.encode('utf8'))
for item in data['GetSignificantDevelopments_Response_1']['FindResponse']['Development']:
list_RepNo=[]
list_DevelopmenId=[]
list_RepNo.append(item['Xrefs']['RepNo'])
list_DevelopmenId.append(item['Xrefs']['DevelopmentId'])
except Exception as Error:
print(Error)
raise
continue
我得到的錯誤如下:
'NoneType' object is not subscriptable
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-c73ee1d65472> in <module>
36 }), headers=headers)
37 data=json.loads(response.text.encode('utf8'))
---> 38 for item in data['GetSignificantDevelopments_Response_1']['FindResponse']['Development']:
39 list_RepNo=[]
40 list_DevelopmenId=[]
TypeError: 'NoneType' object is not subscriptable
我該如何解決?
當您索引的數組是None
類型時會發生這種情況。
在你的情況下,如果你這樣做
In[1]: type(data)
你會得到
Out[1]: <class 'NoneType'>
解決方案:
您必須確保被索引的數組(在您的情況下為數據)不是None
類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.