I am using the GoCardless API and I'm trying to print all the customer records. The code is iterating through each record and I'm successfully pulling data. woop. The issue I have is when trying to pull custom fields these are listed as metadata but the code falls over if the field isn't completed. Instead of the dictionary containing the Key but with a null value, the key just doesn't exist. This means the code falls over as it thinks the key doesn't exist.
I think what I need to do is add an if statement which prints NULL whenever it can't find the Key but I can't get that to work.
Thanks in advance.
The code is below:
import gocardless_pro
import pandas
import Access_key
import os
client = gocardless_pro.Client(
access_token = Access_key.accesskey_live,
environment = 'live'
)
customers = client.customers.list().records
print(client.customers.list().records[0].__dict__)
data = {'id_customer' : [customer.id for customer in customers],
'First Name' : [customer.given_name for customer in customers],
'Family Name' : [customer.family_name for customer in customers],
'address_line1' : [customer.address_line1 for customer in customers],
'address_line2' : [customer.address_line2 for customer in customers],
'address_line3' : [customer.address_line3 for customer in customers],
'City' : [customer.city for customer in customers],
'Region' : [customer.region for customer in customers],
'Post Code' : [customer.postal_code for customer in customers],
'id_tenant' : [customer.metadata["t"] for customer in customers],
'Created date' : [customer.created_at for customer in customers]
}
df = pandas.DataFrame(data)
print(df)