On 21/09/2017 11:18, Sayth Renshaw wrote:
Hi

I have been toying with json and I particular area where I cannot get the 
desired result a list of tuples as my return. The json from the API is way to 
long but I don't think it will matter.

.. hitting url
data = r.json()

for item in data["RaceDay"]['Meetings'][0]['Races']:
     raceDetails = item['RacingFormGuide']['Event']['Race']
     print(raceDetails)

This returns

{'Number': 1, 'NumberDisplay': '01', 'Distance': 1000, 'DistanceDisplay': '1000 
METRES', 'Name': 'CLASS 3 HANDICAP', 'NameForm': 'HIGHWAY-C3'}
{'Number': 2, 'NumberDisplay': '02', 'Distance': 1600, 'DistanceDisplay': '1600 
METRES', 'Name': 'BM 90 HANDICAP', 'NameForm': 'BM90'}
{'Number': 3, 'NumberDisplay': '03', 'Distance': 1100, 'DistanceDisplay': '1100 
METRES', 'Name': 'HERITAGE STAKES', 'NameForm': 'HERITAGE'}
{'Number': 4, 'NumberDisplay': '04', 'Distance': 1400, 'DistanceDisplay': '1400 
METRES', 'Name': 'BILL RITCHIE HANDICAP', 'NameForm': 'RITCHIE'}
{'Number': 5, 'NumberDisplay': '05', 'Distance': 1400, 'DistanceDisplay': '1400 
METRES', 'Name': 'TEA ROSE STAKES', 'NameForm': 'TEA ROSE'}
{'Number': 6, 'NumberDisplay': '06', 'Distance': 1600, 'DistanceDisplay': '1600 
METRES', 'Name': 'GEORGE MAIN STAKES', 'NameForm': 'GEO MAIN'}
{'Number': 7, 'NumberDisplay': '07', 'Distance': 1100, 'DistanceDisplay': '1100 
METRES', 'Name': 'THE SHORTS', 'NameForm': 'THE SHORTS'}
{'Number': 8, 'NumberDisplay': '08', 'Distance': 2000, 'DistanceDisplay': '2000 
METRES', 'Name': 'KINGTON TOWN STAKES', 'NameForm': 'KING TOWN'}
{'Number': 9, 'NumberDisplay': '09', 'Distance': 1200, 'DistanceDisplay': '1200 
METRES', 'Name': 'BM 84 HANDICAP', 'NameForm': 'BM84'}

My goal is to select a few elements and create a list of 3 element tuples
like this
[('CLASS 3 HANDICAP', 1, 1000), ('BM 90 HANDICAP', 2, 1600), ('HERITAGE 
STAKES', 3, 1100), ('BILL RITCHIE HANDICAP', 4, 1400), ('TEA ROSE STAKES', 5, 
1400), ('GEORGE MAIN STAKES', 6, 1600), ('THE SHORTS', 7, 1100), ('KINGTON TOWN 
STAKES', 8, 2000), ('BM 84 HANDICAP', 9, 1200)]

I get close creating a list of elements but each attempt I try to create the 
list of tuples fails.

This is my closest code

data = r.json()

raceData = []

for item in data["RaceDay"]['Meetings'][0]['Races']:
     raceDetails = item['RacingFormGuide']['Event']['Race']
     raceData += 
(raceDetails['Name'],raceDetails['Number'],raceDetails['Distance'])

print(raceDetails)

which returns

['CLASS 3 HANDICAP', 1, 1000, 'BM 90 HANDICAP', 2, 1600, 'HERITAGE STAKES', 3, 
1100, 'BILL RITCHIE HANDICAP', 4, 1400, 'TEA ROSE STAKES', 5, 1400, 'GEORGE 
MAIN STAKES', 6, 1600, 'THE SHORTS', 7, 1100, 'KINGTON TOWN STAKES', 8, 2000, 
'BM 84 HANDICAP', 9, 1200]

How do I get the tuples?

Cheers

Sayth

---
This email has been checked for viruses by AVG.
http://www.avg.com


After a quick glance and hence completely untested:-

raceData.append((raceDetails['Name'],raceDetails['Number'],raceDetails['Distance']))

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to