Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

python 3.x - Fetch Keys from Response Dictionary

Sending Client request to get data from API.

request =client.get_income('ET',incomeType='PNL',startTime=1611287550000) 

The API returns the following data:

[{"symbol":"ET","incomeType":"R","income":"-2.4","time":1611287909000,"info":"50538270"},{"symbol":"ET","incomeType":"R","income":"1.68","time":1611287909000,"info":"50538271"}]

It's a dictionary inside the list. When I try to access the items through a for loop or any of the following methods, It returns the OBJECT.

Methods I tried:

for item in request:
    print(item['income'])

returns this : <model.income.Income object at 0x000000684EFB4580>
print(request[0]['income']
ERROR: TypeError: 'Income' object is not subscriptable

None of them works.

question from:https://stackoverflow.com/questions/65839300/fetch-keys-from-response-dictionary

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have fixed it myself.

request =client.get_income('ET',incomeType='PNL',startTime=1611287550000)

for x in range(len(request)):
    print(request[x].__dict__['income'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...