I am appending data to a list from a dictionary i have scraped. One element is the exchange rate for a given stock. In some cases there is no exchange done on the given stock, so this element is not provided in the dictionary I am scraping and returns 'None'.
I am trying to introduce an if statement in my for loop to append the value 1 (could be anything, it doesn't matter) if the object is NoneType however I keep getting the same error.
data_list=[]
for item in data['data']:
#print(item)
order_details = trading212.get_order_details(item['detailsPath'])
#print(order_details)
print(item['heading']['context']['prettyName'])
#print(order_details['sections'][3]['rows'][1]['value']['context']['amount'])
data_list.append([item['heading']['context']['prettyName'], #Name
item['heading']['context']['instrument'], #Ticker
item['subHeading']['context']['quantity'], #Qty
item['mainInfo']['context']['amount'], #Amount paid
item['mainInfo']['context']['currency'], #Currency paid
order_details['sections'][3]['rows'][0]['value']['context']['amount'], #exchange_fee
order_details['sections'][2]['rows'][1]['value']['context']['quantity'], #filled qty
order_details['sections'][2]['rows'][2]['value']['context']['amount'], #Stock Price/ Fill Price
order_details['sections'][2]['rows'][2]['value']['context']['currency'], #Currency of Stock
item['date']])
if order_details['sections'][2]['rows'][4]['value']['context']['quantity'] is not None:
data_list.append(order_details['sections'][2]['rows'][4]['value']['context']['quantity']) #exchange_rate)
else:
data_list.append(1)
I get the following error message:
if order_details['sections'][2]['rows'][4]['value']['context']['quantity'] is not None: TypeError: 'NoneType' object is not subscriptable