3
For loop only picking last item in this json object
Post Body
The following code..
Arsenal = json.load(urllib2.urlopen("http://api.football-data.org/alpha/teams/57/players"))
for team in Arsenal['players']:
print team['name'], team['dateOfBirth'], team['nationality'], team['position']
Returns this list:
Wojciech Szczesny 1990-04-18 Poland Keeper
David Ospina 1988-08-31 Colombia Keeper
Damián MartÃnez 1992-09-02 Argentina Keeper
Per Mertesacker 1984-09-29 Germany Centre Back
Laurent Koscielny 1985-09-10 France Centre Back
Kieran Gibbs 1989-09-26 England Left-Back
Nacho Monreal 1986-02-26 Spain Left-Back
Mathieu Debuchy 1985-07-28 France Right-Back
Calum Chambers 1995-01-20 England Right-Back
Héctor BellerÃn 1995-03-10 Spain Right-Back
Mathieu Flamini 1984-03-07 France Defensive Midfield
Mikel Arteta 1982-03-26 Spain Defensive Midfield
Francis Coquelin 1991-05-13 France Defensive Midfield
Jack Wilshere 1992-01-01 England Central Midfield
Aaron Ramsey 1990-12-26 Wales Central Midfield
Abou Diaby 1986-05-11 France Central Midfield
Mesut Özil 1988-10-15 Germany Attacking Midfield
Tomás Rosicky 1980-10-04 Czech Republic Attacking Midf
Santi Cazorla 1984-12-13 Spain Left Wing
Alex Oxlade-Chamberlain 1993-08-15 England Right Wing
Serge Gnabry 1995-07-14 Germany Right Wing
Alexis Sánchez 1988-12-19 Chile Right Wing
Theo Walcott 1989-03-16 England Right Wing
Olivier Giroud 1986-09-30 France Centre Forward
Danny Welbeck 1990-11-26 England Centre Forward
Gabriel Paulista 1990-11-26 Brazil Centre Back
Krystian Bielik 1998-01-04 Poland Defensive Midfield
However, when I try and run the following for loop I only get the last item on the list returned 'Krystian Bielik'. This is my for loop:
from teams.models import clubs, players
club1 = "Arsenal"
club2 = clubs.objects.get(name = club1);
def add_players(name, position, born, nationality, club, active=True):
# run the script per club
p = players.objects.get_or_create(name=name, position=position, born=born, nationality=nationality, club=club)
return p
Arsenal = json.load(urllib2.urlopen("http://api.football-data.org/alpha/teams/57/players"))
for team in Arsenal['players']:
name1 = team['name']
position1 = team['position']
born1 = team['dateOfBirth']
nationality1 = team['nationality']
def populate():
add_players(
name = name1,
position = position1,
born = born1,
nationality = nationality1,
club = club2
)
Hopefully this is enough info, can anyone advise what I'd need to do with my for loop? I did try a few things but with no luck.
Author
Account Strength
100%
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
14,533
Link Karma
10,604
Comment Karma
3,929
Profile updated: 1 week ago
Posts updated: 10 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 9 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnpython...