How can I access elements of the retrieved list using Python (from Harvest)

73 Views Asked by At

type(harvest.clients()) output: list

harvest.clients()[0] output:

OrderedDict([(u'client',
              OrderedDict([(u'id', 2793223),
                           (u'name', u'1 TEMPLATES'),
                           (u'active', True),
                           (u'currency', u'Australian Dollar - AUD'),
                           (u'updated_at', u'2014-09-14T22:48:29Z'),
                           (u'created_at', u'2014-09-14T22:48:29Z'),
                           (u'default_invoice_timeframe', None),
                           (u'address', u''),
                           (u'currency_symbol', u'$'),
                           (u'details', u''),
                           (u'last_invoice_kind', None)]))]

How can I access client id, name, active, currency etc ?

2

There are 2 best solutions below

0
On
client = harvest.clients()[0]['client']
print(client['id'])
print(client['name'])
print(client['active'])
print(client['currency'])

Refer to https://docs.python.org/3/library/collections.html#collections.OrderedDict

0
On

Try the following code to loop your ordereddict:

for key,value in o['client'].items():
    print(key,value)

Output:

id 2793223
name 1 TEMPLATES
active True
currency Australian Dollar - AUD
updated_at 2014-09-14T22:48:29Z
created_at 2014-09-14T22:48:29Z
default_invoice_timeframe None
address 
currency_symbol $
details 
last_invoice_kind None