In general, the csv
module is an easy way of working with delimited data -- the 'delimiter' is most often a comma, but can be other things as well (e.g., space, tab, semicolon). This is one of the most common file formats you will encounter.
Now we can do this without (as we have been doing all semester)...
with open('qbdata.txt') as f:
data = f.readlines()
header = data[0].split()
data = [row.split() for row in data[1:]]
print(header)
data
['First', 'Last', 'Position', 'Team', 'Completions', 'Attempts', 'Yards', 'TDs', 'Ints', 'Comp%', 'Rating']
[['Colt', 'McCoy', 'QB', 'CLE', '135', '222', '1576', '6', '9', '60.8%', '74.5'], ['Josh', 'Freeman', 'QB', 'TB', '291', '474', '3451', '25', '6', '61.4%', '95.9'], ['Michael', 'Vick', 'QB', 'PHI', '233', '372', '3018', '21', '6', '62.6%', '100.2'], ['Matt', 'Schaub', 'QB', 'HOU', '365', '574', '4370', '24', '12', '63.6%', '92.0'], ['Philip', 'Rivers', 'QB', 'SD', '357', '541', '4710', '30', '13', '66.0%', '101.8'], ['Matt', 'Hasselbeck', 'QB', 'SEA', '266', '444', '3001', '12', '17', '59.9%', '73.2'], ['Jimmy', 'Clausen', 'QB', 'CAR', '157', '299', '1558', '3', '9', '52.5%', '58.4'], ['Joe', 'Flacco', 'QB', 'BAL', '306', '489', '3622', '25', '10', '62.6%', '93.6'], ['Kyle', 'Orton', 'QB', 'DEN', '293', '498', '3653', '20', '9', '58.8%', '87.5'], ['Jason', 'Campbell', 'QB', 'OAK', '194', '329', '2387', '13', '8', '59.0%', '84.5'], ['Peyton', 'Manning', 'QB', 'IND', '450', '679', '4700', '33', '17', '66.3%', '91.9'], ['Drew', 'Brees', 'QB', 'NO', '448', '658', '4620', '33', '22', '68.1%', '90.9'], ['Matt', 'Ryan', 'QB', 'ATL', '357', '571', '3705', '28', '9', '62.5%', '91.0'], ['Matt', 'Cassel', 'QB', 'KC', '262', '450', '3116', '27', '7', '58.2%', '93.0'], ['Mark', 'Sanchez', 'QB', 'NYJ', '278', '507', '3291', '17', '13', '54.8%', '75.3'], ['Brett', 'Favre', 'QB', 'MIN', '217', '358', '2509', '11', '19', '60.6%', '69.9'], ['David', 'Garrard', 'QB', 'JAC', '236', '366', '2734', '23', '15', '64.5%', '90.8'], ['Eli', 'Manning', 'QB', 'NYG', '339', '539', '4002', '31', '25', '62.9%', '85.3'], ['Carson', 'Palmer', 'QB', 'CIN', '362', '586', '3970', '26', '20', '61.8%', '82.4'], ['Alex', 'Smith', 'QB', 'SF', '204', '342', '2370', '14', '10', '59.6%', '82.1'], ['Chad', 'Henne', 'QB', 'MIA', '301', '490', '3301', '15', '19', '61.4%', '75.4'], ['Tony', 'Romo', 'QB', 'DAL', '148', '213', '1605', '11', '7', '69.5%', '94.9'], ['Jay', 'Cutler', 'QB', 'CHI', '261', '432', '3274', '23', '16', '60.4%', '86.3'], ['Jon', 'Kitna', 'QB', 'DAL', '209', '318', '2365', '16', '12', '65.7%', '88.9'], ['Tom', 'Brady', 'QB', 'NE', '324', '492', '3900', '36', '4', '65.9%', '111.0'], ['Ben', 'Roethlisberger', 'QB', 'PIT', '240', '389', '3200', '17', '5', '61.7%', '97.0'], ['Kerry', 'Collins', 'QB', 'TEN', '160', '278', '1823', '14', '8', '57.6%', '82.2'], ['Derek', 'Anderson', 'QB', 'ARI', '169', '327', '2065', '7', '10', '51.7%', '65.9'], ['Ryan', 'Fitzpatrick', 'QB', 'BUF', '255', '441', '3000', '23', '15', '57.8%', '81.8'], ['Donovan', 'McNabb', 'QB', 'WAS', '275', '472', '3377', '14', '15', '58.3%', '77.1'], ['Kevin', 'Kolb', 'QB', 'PHI', '115', '189', '1197', '7', '7', '60.8%', '76.1'], ['Aaron', 'Rodgers', 'QB', 'GB', '312', '475', '3922', '28', '11', '65.7%', '101.2'], ['Sam', 'Bradford', 'QB', 'STL', '354', '590', '3512', '18', '15', '60.0%', '76.5'], ['Shaun', 'Hill', 'QB', 'DET', '257', '416', '2686', '16', '12', '61.8%', '81.3']]
But it can become tedious, particularly as small options change between files/programs...
with open('qbdata.csv') as f:
data = f.readlines()
header = data[0].strip().split(',')
data = [row.strip().split(',') for row in data[1:]]
print(header)
data
['First', 'Last', 'Position', 'Team', 'Completions', 'Attempts', 'Yards', 'TDs', 'Ints', 'Comp%', 'Rating']
[['Colt', 'McCoy', 'QB', 'CLE', '135', '222', '1576', '6', '9', '60.80%', '74.5'], ['Josh', 'Freeman', 'QB', 'TB', '291', '474', '3451', '25', '6', '61.40%', '95.9'], ['Michael', 'Vick', 'QB', 'PHI', '233', '372', '3018', '21', '6', '62.60%', '100.2'], ['Matt', 'Schaub', 'QB', 'HOU', '365', '574', '4370', '24', '12', '63.60%', '92'], ['Philip', 'Rivers', 'QB', 'SD', '357', '541', '4710', '30', '13', '66.00%', '101.8'], ['Matt', 'Hasselbeck', 'QB', 'SEA', '266', '444', '3001', '12', '17', '59.90%', '73.2'], ['Jimmy', 'Clausen', 'QB', 'CAR', '157', '299', '1558', '3', '9', '52.50%', '58.4'], ['Joe', 'Flacco', 'QB', 'BAL', '306', '489', '3622', '25', '10', '62.60%', '93.6'], ['Kyle', 'Orton', 'QB', 'DEN', '293', '498', '3653', '20', '9', '58.80%', '87.5'], ['Jason', 'Campbell', 'QB', 'OAK', '194', '329', '2387', '13', '8', '59.00%', '84.5'], ['Peyton', 'Manning', 'QB', 'IND', '450', '679', '4700', '33', '17', '66.30%', '91.9'], ['Drew', 'Brees', 'QB', 'NO', '448', '658', '4620', '33', '22', '68.10%', '90.9'], ['Matt', 'Ryan', 'QB', 'ATL', '357', '571', '3705', '28', '9', '62.50%', '91'], ['Matt', 'Cassel', 'QB', 'KC', '262', '450', '3116', '27', '7', '58.20%', '93'], ['Mark', 'Sanchez', 'QB', 'NYJ', '278', '507', '3291', '17', '13', '54.80%', '75.3'], ['Brett', 'Favre', 'QB', 'MIN', '217', '358', '2509', '11', '19', '60.60%', '69.9'], ['David', 'Garrard', 'QB', 'JAC', '236', '366', '2734', '23', '15', '64.50%', '90.8'], ['Eli', 'Manning', 'QB', 'NYG', '339', '539', '4002', '31', '25', '62.90%', '85.3'], ['Carson', 'Palmer', 'QB', 'CIN', '362', '586', '3970', '26', '20', '61.80%', '82.4'], ['Alex', 'Smith', 'QB', 'SF', '204', '342', '2370', '14', '10', '59.60%', '82.1'], ['Chad', 'Henne', 'QB', 'MIA', '301', '490', '3301', '15', '19', '61.40%', '75.4'], ['Tony', 'Romo', 'QB', 'DAL', '148', '213', '1605', '11', '7', '69.50%', '94.9'], ['Jay', 'Cutler', 'QB', 'CHI', '261', '432', '3274', '23', '16', '60.40%', '86.3'], ['Jon', 'Kitna', 'QB', 'DAL', '209', '318', '2365', '16', '12', '65.70%', '88.9'], ['Tom', 'Brady', 'QB', 'NE', '324', '492', '3900', '36', '4', '65.90%', '111'], ['Ben', 'Roethlisberger', 'QB', 'PIT', '240', '389', '3200', '17', '5', '61.70%', '97'], ['Kerry', 'Collins', 'QB', 'TEN', '160', '278', '1823', '14', '8', '57.60%', '82.2'], ['Derek', 'Anderson', 'QB', 'ARI', '169', '327', '2065', '7', '10', '51.70%', '65.9'], ['Ryan', 'Fitzpatrick', 'QB', 'BUF', '255', '441', '3000', '23', '15', '57.80%', '81.8'], ['Donovan', 'McNabb', 'QB', 'WAS', '275', '472', '3377', '14', '15', '58.30%', '77.1'], ['Kevin', 'Kolb', 'QB', 'PHI', '115', '189', '1197', '7', '7', '60.80%', '76.1'], ['Aaron', 'Rodgers', 'QB', 'GB', '312', '475', '3922', '28', '11', '65.70%', '101.2'], ['Sam', 'Bradford', 'QB', 'STL', '354', '590', '3512', '18', '15', '60.00%', '76.5'], ['Shaun', 'Hill', 'QB', 'DET', '257', '416', '2686', '16', '12', '61.80%', '81.3']]
The csv
module simplifies matters and gives nice utility functions. Read the documentation for more details, but here's an introduction...
import csv
with open("qbdata.txt") as f:
reader = csv.reader(f, delimiter=' ', skipinitialspace=True)
header = next(reader)
data = list(reader)
print(header)
data
['First', 'Last', 'Position', 'Team', 'Completions', 'Attempts', 'Yards', 'TDs', 'Ints', 'Comp%', 'Rating']
[['Colt', 'McCoy', 'QB', 'CLE', '135', '222', '1576', '6', '9', '60.8%', '74.5'], ['Josh', 'Freeman', 'QB', 'TB', '291', '474', '3451', '25', '6', '61.4%', '95.9'], ['Michael', 'Vick', 'QB', 'PHI', '233', '372', '3018', '21', '6', '62.6%', '100.2'], ['Matt', 'Schaub', 'QB', 'HOU', '365', '574', '4370', '24', '12', '63.6%', '92.0'], ['Philip', 'Rivers', 'QB', 'SD', '357', '541', '4710', '30', '13', '66.0%', '101.8'], ['Matt', 'Hasselbeck', 'QB', 'SEA', '266', '444', '3001', '12', '17', '59.9%', '73.2'], ['Jimmy', 'Clausen', 'QB', 'CAR', '157', '299', '1558', '3', '9', '52.5%', '58.4'], ['Joe', 'Flacco', 'QB', 'BAL', '306', '489', '3622', '25', '10', '62.6%', '93.6'], ['Kyle', 'Orton', 'QB', 'DEN', '293', '498', '3653', '20', '9', '58.8%', '87.5'], ['Jason', 'Campbell', 'QB', 'OAK', '194', '329', '2387', '13', '8', '59.0%', '84.5'], ['Peyton', 'Manning', 'QB', 'IND', '450', '679', '4700', '33', '17', '66.3%', '91.9'], ['Drew', 'Brees', 'QB', 'NO', '448', '658', '4620', '33', '22', '68.1%', '90.9'], ['Matt', 'Ryan', 'QB', 'ATL', '357', '571', '3705', '28', '9', '62.5%', '91.0'], ['Matt', 'Cassel', 'QB', 'KC', '262', '450', '3116', '27', '7', '58.2%', '93.0'], ['Mark', 'Sanchez', 'QB', 'NYJ', '278', '507', '3291', '17', '13', '54.8%', '75.3'], ['Brett', 'Favre', 'QB', 'MIN', '217', '358', '2509', '11', '19', '60.6%', '69.9'], ['David', 'Garrard', 'QB', 'JAC', '236', '366', '2734', '23', '15', '64.5%', '90.8'], ['Eli', 'Manning', 'QB', 'NYG', '339', '539', '4002', '31', '25', '62.9%', '85.3'], ['Carson', 'Palmer', 'QB', 'CIN', '362', '586', '3970', '26', '20', '61.8%', '82.4'], ['Alex', 'Smith', 'QB', 'SF', '204', '342', '2370', '14', '10', '59.6%', '82.1'], ['Chad', 'Henne', 'QB', 'MIA', '301', '490', '3301', '15', '19', '61.4%', '75.4'], ['Tony', 'Romo', 'QB', 'DAL', '148', '213', '1605', '11', '7', '69.5%', '94.9'], ['Jay', 'Cutler', 'QB', 'CHI', '261', '432', '3274', '23', '16', '60.4%', '86.3'], ['Jon', 'Kitna', 'QB', 'DAL', '209', '318', '2365', '16', '12', '65.7%', '88.9'], ['Tom', 'Brady', 'QB', 'NE', '324', '492', '3900', '36', '4', '65.9%', '111.0'], ['Ben', 'Roethlisberger', 'QB', 'PIT', '240', '389', '3200', '17', '5', '61.7%', '97.0'], ['Kerry', 'Collins', 'QB', 'TEN', '160', '278', '1823', '14', '8', '57.6%', '82.2'], ['Derek', 'Anderson', 'QB', 'ARI', '169', '327', '2065', '7', '10', '51.7%', '65.9'], ['Ryan', 'Fitzpatrick', 'QB', 'BUF', '255', '441', '3000', '23', '15', '57.8%', '81.8'], ['Donovan', 'McNabb', 'QB', 'WAS', '275', '472', '3377', '14', '15', '58.3%', '77.1'], ['Kevin', 'Kolb', 'QB', 'PHI', '115', '189', '1197', '7', '7', '60.8%', '76.1'], ['Aaron', 'Rodgers', 'QB', 'GB', '312', '475', '3922', '28', '11', '65.7%', '101.2'], ['Sam', 'Bradford', 'QB', 'STL', '354', '590', '3512', '18', '15', '60.0%', '76.5'], ['Shaun', 'Hill', 'QB', 'DET', '257', '416', '2686', '16', '12', '61.8%', '81.3']]
with open("qbdata.csv") as f:
reader = csv.reader(f, delimiter=',', skipinitialspace=True)
header = next(reader)
data = list(reader)
print(header)
data
['First', 'Last', 'Position', 'Team', 'Completions', 'Attempts', 'Yards', 'TDs', 'Ints', 'Comp%', 'Rating']
[['Colt', 'McCoy', 'QB', 'CLE', '135', '222', '1576', '6', '9', '60.80%', '74.5'], ['Josh', 'Freeman', 'QB', 'TB', '291', '474', '3451', '25', '6', '61.40%', '95.9'], ['Michael', 'Vick', 'QB', 'PHI', '233', '372', '3018', '21', '6', '62.60%', '100.2'], ['Matt', 'Schaub', 'QB', 'HOU', '365', '574', '4370', '24', '12', '63.60%', '92'], ['Philip', 'Rivers', 'QB', 'SD', '357', '541', '4710', '30', '13', '66.00%', '101.8'], ['Matt', 'Hasselbeck', 'QB', 'SEA', '266', '444', '3001', '12', '17', '59.90%', '73.2'], ['Jimmy', 'Clausen', 'QB', 'CAR', '157', '299', '1558', '3', '9', '52.50%', '58.4'], ['Joe', 'Flacco', 'QB', 'BAL', '306', '489', '3622', '25', '10', '62.60%', '93.6'], ['Kyle', 'Orton', 'QB', 'DEN', '293', '498', '3653', '20', '9', '58.80%', '87.5'], ['Jason', 'Campbell', 'QB', 'OAK', '194', '329', '2387', '13', '8', '59.00%', '84.5'], ['Peyton', 'Manning', 'QB', 'IND', '450', '679', '4700', '33', '17', '66.30%', '91.9'], ['Drew', 'Brees', 'QB', 'NO', '448', '658', '4620', '33', '22', '68.10%', '90.9'], ['Matt', 'Ryan', 'QB', 'ATL', '357', '571', '3705', '28', '9', '62.50%', '91'], ['Matt', 'Cassel', 'QB', 'KC', '262', '450', '3116', '27', '7', '58.20%', '93'], ['Mark', 'Sanchez', 'QB', 'NYJ', '278', '507', '3291', '17', '13', '54.80%', '75.3'], ['Brett', 'Favre', 'QB', 'MIN', '217', '358', '2509', '11', '19', '60.60%', '69.9'], ['David', 'Garrard', 'QB', 'JAC', '236', '366', '2734', '23', '15', '64.50%', '90.8'], ['Eli', 'Manning', 'QB', 'NYG', '339', '539', '4002', '31', '25', '62.90%', '85.3'], ['Carson', 'Palmer', 'QB', 'CIN', '362', '586', '3970', '26', '20', '61.80%', '82.4'], ['Alex', 'Smith', 'QB', 'SF', '204', '342', '2370', '14', '10', '59.60%', '82.1'], ['Chad', 'Henne', 'QB', 'MIA', '301', '490', '3301', '15', '19', '61.40%', '75.4'], ['Tony', 'Romo', 'QB', 'DAL', '148', '213', '1605', '11', '7', '69.50%', '94.9'], ['Jay', 'Cutler', 'QB', 'CHI', '261', '432', '3274', '23', '16', '60.40%', '86.3'], ['Jon', 'Kitna', 'QB', 'DAL', '209', '318', '2365', '16', '12', '65.70%', '88.9'], ['Tom', 'Brady', 'QB', 'NE', '324', '492', '3900', '36', '4', '65.90%', '111'], ['Ben', 'Roethlisberger', 'QB', 'PIT', '240', '389', '3200', '17', '5', '61.70%', '97'], ['Kerry', 'Collins', 'QB', 'TEN', '160', '278', '1823', '14', '8', '57.60%', '82.2'], ['Derek', 'Anderson', 'QB', 'ARI', '169', '327', '2065', '7', '10', '51.70%', '65.9'], ['Ryan', 'Fitzpatrick', 'QB', 'BUF', '255', '441', '3000', '23', '15', '57.80%', '81.8'], ['Donovan', 'McNabb', 'QB', 'WAS', '275', '472', '3377', '14', '15', '58.30%', '77.1'], ['Kevin', 'Kolb', 'QB', 'PHI', '115', '189', '1197', '7', '7', '60.80%', '76.1'], ['Aaron', 'Rodgers', 'QB', 'GB', '312', '475', '3922', '28', '11', '65.70%', '101.2'], ['Sam', 'Bradford', 'QB', 'STL', '354', '590', '3512', '18', '15', '60.00%', '76.5'], ['Shaun', 'Hill', 'QB', 'DET', '257', '416', '2686', '16', '12', '61.80%', '81.3']]
with open("qbdata.csv") as f:
reader = csv.DictReader(f)
data = list(reader)
print(data[0]['Attempts'])
data
222
[OrderedDict([('First', 'Colt'), ('Last', 'McCoy'), ('Position', 'QB'), ('Team', 'CLE'), ('Completions', '135'), ('Attempts', '222'), ('Yards', '1576'), ('TDs', '6'), ('Ints', '9'), ('Comp%', '60.80%'), ('Rating', '74.5')]), OrderedDict([('First', 'Josh'), ('Last', 'Freeman'), ('Position', 'QB'), ('Team', 'TB'), ('Completions', '291'), ('Attempts', '474'), ('Yards', '3451'), ('TDs', '25'), ('Ints', '6'), ('Comp%', '61.40%'), ('Rating', '95.9')]), OrderedDict([('First', 'Michael'), ('Last', 'Vick'), ('Position', 'QB'), ('Team', 'PHI'), ('Completions', '233'), ('Attempts', '372'), ('Yards', '3018'), ('TDs', '21'), ('Ints', '6'), ('Comp%', '62.60%'), ('Rating', '100.2')]), OrderedDict([('First', 'Matt'), ('Last', 'Schaub'), ('Position', 'QB'), ('Team', 'HOU'), ('Completions', '365'), ('Attempts', '574'), ('Yards', '4370'), ('TDs', '24'), ('Ints', '12'), ('Comp%', '63.60%'), ('Rating', '92')]), OrderedDict([('First', 'Philip'), ('Last', 'Rivers'), ('Position', 'QB'), ('Team', 'SD'), ('Completions', '357'), ('Attempts', '541'), ('Yards', '4710'), ('TDs', '30'), ('Ints', '13'), ('Comp%', '66.00%'), ('Rating', '101.8')]), OrderedDict([('First', 'Matt'), ('Last', 'Hasselbeck'), ('Position', 'QB'), ('Team', 'SEA'), ('Completions', '266'), ('Attempts', '444'), ('Yards', '3001'), ('TDs', '12'), ('Ints', '17'), ('Comp%', '59.90%'), ('Rating', '73.2')]), OrderedDict([('First', 'Jimmy'), ('Last', 'Clausen'), ('Position', 'QB'), ('Team', 'CAR'), ('Completions', '157'), ('Attempts', '299'), ('Yards', '1558'), ('TDs', '3'), ('Ints', '9'), ('Comp%', '52.50%'), ('Rating', '58.4')]), OrderedDict([('First', 'Joe'), ('Last', 'Flacco'), ('Position', 'QB'), ('Team', 'BAL'), ('Completions', '306'), ('Attempts', '489'), ('Yards', '3622'), ('TDs', '25'), ('Ints', '10'), ('Comp%', '62.60%'), ('Rating', '93.6')]), OrderedDict([('First', 'Kyle'), ('Last', 'Orton'), ('Position', 'QB'), ('Team', 'DEN'), ('Completions', '293'), ('Attempts', '498'), ('Yards', '3653'), ('TDs', '20'), ('Ints', '9'), ('Comp%', '58.80%'), ('Rating', '87.5')]), OrderedDict([('First', 'Jason'), ('Last', 'Campbell'), ('Position', 'QB'), ('Team', 'OAK'), ('Completions', '194'), ('Attempts', '329'), ('Yards', '2387'), ('TDs', '13'), ('Ints', '8'), ('Comp%', '59.00%'), ('Rating', '84.5')]), OrderedDict([('First', 'Peyton'), ('Last', 'Manning'), ('Position', 'QB'), ('Team', 'IND'), ('Completions', '450'), ('Attempts', '679'), ('Yards', '4700'), ('TDs', '33'), ('Ints', '17'), ('Comp%', '66.30%'), ('Rating', '91.9')]), OrderedDict([('First', 'Drew'), ('Last', 'Brees'), ('Position', 'QB'), ('Team', 'NO'), ('Completions', '448'), ('Attempts', '658'), ('Yards', '4620'), ('TDs', '33'), ('Ints', '22'), ('Comp%', '68.10%'), ('Rating', '90.9')]), OrderedDict([('First', 'Matt'), ('Last', 'Ryan'), ('Position', 'QB'), ('Team', 'ATL'), ('Completions', '357'), ('Attempts', '571'), ('Yards', '3705'), ('TDs', '28'), ('Ints', '9'), ('Comp%', '62.50%'), ('Rating', '91')]), OrderedDict([('First', 'Matt'), ('Last', 'Cassel'), ('Position', 'QB'), ('Team', 'KC'), ('Completions', '262'), ('Attempts', '450'), ('Yards', '3116'), ('TDs', '27'), ('Ints', '7'), ('Comp%', '58.20%'), ('Rating', '93')]), OrderedDict([('First', 'Mark'), ('Last', 'Sanchez'), ('Position', 'QB'), ('Team', 'NYJ'), ('Completions', '278'), ('Attempts', '507'), ('Yards', '3291'), ('TDs', '17'), ('Ints', '13'), ('Comp%', '54.80%'), ('Rating', '75.3')]), OrderedDict([('First', 'Brett'), ('Last', 'Favre'), ('Position', 'QB'), ('Team', 'MIN'), ('Completions', '217'), ('Attempts', '358'), ('Yards', '2509'), ('TDs', '11'), ('Ints', '19'), ('Comp%', '60.60%'), ('Rating', '69.9')]), OrderedDict([('First', 'David'), ('Last', 'Garrard'), ('Position', 'QB'), ('Team', 'JAC'), ('Completions', '236'), ('Attempts', '366'), ('Yards', '2734'), ('TDs', '23'), ('Ints', '15'), ('Comp%', '64.50%'), ('Rating', '90.8')]), OrderedDict([('First', 'Eli'), ('Last', 'Manning'), ('Position', 'QB'), ('Team', 'NYG'), ('Completions', '339'), ('Attempts', '539'), ('Yards', '4002'), ('TDs', '31'), ('Ints', '25'), ('Comp%', '62.90%'), ('Rating', '85.3')]), OrderedDict([('First', 'Carson'), ('Last', 'Palmer'), ('Position', 'QB'), ('Team', 'CIN'), ('Completions', '362'), ('Attempts', '586'), ('Yards', '3970'), ('TDs', '26'), ('Ints', '20'), ('Comp%', '61.80%'), ('Rating', '82.4')]), OrderedDict([('First', 'Alex'), ('Last', 'Smith'), ('Position', 'QB'), ('Team', 'SF'), ('Completions', '204'), ('Attempts', '342'), ('Yards', '2370'), ('TDs', '14'), ('Ints', '10'), ('Comp%', '59.60%'), ('Rating', '82.1')]), OrderedDict([('First', 'Chad'), ('Last', 'Henne'), ('Position', 'QB'), ('Team', 'MIA'), ('Completions', '301'), ('Attempts', '490'), ('Yards', '3301'), ('TDs', '15'), ('Ints', '19'), ('Comp%', '61.40%'), ('Rating', '75.4')]), OrderedDict([('First', 'Tony'), ('Last', 'Romo'), ('Position', 'QB'), ('Team', 'DAL'), ('Completions', '148'), ('Attempts', '213'), ('Yards', '1605'), ('TDs', '11'), ('Ints', '7'), ('Comp%', '69.50%'), ('Rating', '94.9')]), OrderedDict([('First', 'Jay'), ('Last', 'Cutler'), ('Position', 'QB'), ('Team', 'CHI'), ('Completions', '261'), ('Attempts', '432'), ('Yards', '3274'), ('TDs', '23'), ('Ints', '16'), ('Comp%', '60.40%'), ('Rating', '86.3')]), OrderedDict([('First', 'Jon'), ('Last', 'Kitna'), ('Position', 'QB'), ('Team', 'DAL'), ('Completions', '209'), ('Attempts', '318'), ('Yards', '2365'), ('TDs', '16'), ('Ints', '12'), ('Comp%', '65.70%'), ('Rating', '88.9')]), OrderedDict([('First', 'Tom'), ('Last', 'Brady'), ('Position', 'QB'), ('Team', 'NE'), ('Completions', '324'), ('Attempts', '492'), ('Yards', '3900'), ('TDs', '36'), ('Ints', '4'), ('Comp%', '65.90%'), ('Rating', '111')]), OrderedDict([('First', 'Ben'), ('Last', 'Roethlisberger'), ('Position', 'QB'), ('Team', 'PIT'), ('Completions', '240'), ('Attempts', '389'), ('Yards', '3200'), ('TDs', '17'), ('Ints', '5'), ('Comp%', '61.70%'), ('Rating', '97')]), OrderedDict([('First', 'Kerry'), ('Last', 'Collins'), ('Position', 'QB'), ('Team', 'TEN'), ('Completions', '160'), ('Attempts', '278'), ('Yards', '1823'), ('TDs', '14'), ('Ints', '8'), ('Comp%', '57.60%'), ('Rating', '82.2')]), OrderedDict([('First', 'Derek'), ('Last', 'Anderson'), ('Position', 'QB'), ('Team', 'ARI'), ('Completions', '169'), ('Attempts', '327'), ('Yards', '2065'), ('TDs', '7'), ('Ints', '10'), ('Comp%', '51.70%'), ('Rating', '65.9')]), OrderedDict([('First', 'Ryan'), ('Last', 'Fitzpatrick'), ('Position', 'QB'), ('Team', 'BUF'), ('Completions', '255'), ('Attempts', '441'), ('Yards', '3000'), ('TDs', '23'), ('Ints', '15'), ('Comp%', '57.80%'), ('Rating', '81.8')]), OrderedDict([('First', 'Donovan'), ('Last', 'McNabb'), ('Position', 'QB'), ('Team', 'WAS'), ('Completions', '275'), ('Attempts', '472'), ('Yards', '3377'), ('TDs', '14'), ('Ints', '15'), ('Comp%', '58.30%'), ('Rating', '77.1')]), OrderedDict([('First', 'Kevin'), ('Last', 'Kolb'), ('Position', 'QB'), ('Team', 'PHI'), ('Completions', '115'), ('Attempts', '189'), ('Yards', '1197'), ('TDs', '7'), ('Ints', '7'), ('Comp%', '60.80%'), ('Rating', '76.1')]), OrderedDict([('First', 'Aaron'), ('Last', 'Rodgers'), ('Position', 'QB'), ('Team', 'GB'), ('Completions', '312'), ('Attempts', '475'), ('Yards', '3922'), ('TDs', '28'), ('Ints', '11'), ('Comp%', '65.70%'), ('Rating', '101.2')]), OrderedDict([('First', 'Sam'), ('Last', 'Bradford'), ('Position', 'QB'), ('Team', 'STL'), ('Completions', '354'), ('Attempts', '590'), ('Yards', '3512'), ('TDs', '18'), ('Ints', '15'), ('Comp%', '60.00%'), ('Rating', '76.5')]), OrderedDict([('First', 'Shaun'), ('Last', 'Hill'), ('Position', 'QB'), ('Team', 'DET'), ('Completions', '257'), ('Attempts', '416'), ('Yards', '2686'), ('TDs', '16'), ('Ints', '12'), ('Comp%', '61.80%'), ('Rating', '81.3')])]
Application Programming Interfaces (APIs) provide a general means of interacting with other programs. More specifically, many websites will provide data via a web API -- this means we can make requests to remote applications / websites for data. Here's what this looks like schematically.
The way that we do this in Python is via the requests
module. To get started, you'll typically need to know...
You then receive back a "response" back from the web API. First, check to make sure the request was successful (usually status code 200). Then, look to the response data, which is commonly formatted in JSON (JavaScript Object Notation), which will look very similar to lists and dictionaries in Python.
Step 1: read the documentation (https://www.yelp.com/developers/documentation/v3/get_started)
Step 2: get an API key
Step 3: Go!
import requests
__API_KEY = ''
__API_URL = 'https://api.yelp.com/v3/businesses/search'
myheaders = {'Authorization' : 'Bearer {}'.format(__API_KEY)}
myparams = {'location':'Northeastern University', 'term':'Sushi'}
response = requests.get(__API_URL, params=myparams, headers=myheaders)
code = response.status_code
if code == 200:
data = response.json()
else:
data = ":("
data
{'businesses': [{'id': 'xFEyKgGkgOycAsJvVsUzVw', 'alias': 'douzo-boston', 'name': 'Douzo', 'image_url': 'https://s3-media2.fl.yelpcdn.com/bphoto/PAHPOJ4rGz2lAgdPANTezA/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/douzo-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 1005, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}], 'rating': 4.0, 'coordinates': {'latitude': 42.3468979763336, 'longitude': -71.0755712773849}, 'transactions': ['delivery'], 'price': '$$$', 'location': {'address1': '131 Dartmouth St', 'address2': None, 'address3': '', 'city': 'Boston', 'zip_code': '02116', 'country': 'US', 'state': 'MA', 'display_address': ['131 Dartmouth St', 'Boston, MA 02116']}, 'phone': '+16178598886', 'display_phone': '(617) 859-8886', 'distance': 1366.0948486989794}, {'id': '4QSm0iwLR27feUtvY3nQ1Q', 'alias': 'sushi-kappo-boston', 'name': 'Sushi Kappo', 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/8CraS6R0jxLQmEmot2Mj_A/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/sushi-kappo-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 61, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'poke', 'title': 'Poke'}, {'alias': 'ramen', 'title': 'Ramen'}], 'rating': 4.5, 'coordinates': {'latitude': 42.34311, 'longitude': -71.09889}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '86 Peterborough St', 'address2': '', 'address3': None, 'city': 'Boston', 'zip_code': '02215', 'country': 'US', 'state': 'MA', 'display_address': ['86 Peterborough St', 'Boston, MA 02215']}, 'phone': '+18572638168', 'display_phone': '(857) 263-8168', 'distance': 886.8865969377729}, {'id': 'Z6Io2AbJrof7TPVliMZkAg', 'alias': 'fuji-at-ink-block-boston', 'name': 'Fuji at Ink Block', 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/G3LRaC_3WLYWSdvQmFkB5w/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/fuji-at-ink-block-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 331, 'categories': [{'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'sushi', 'title': 'Sushi Bars'}], 'rating': 4.5, 'coordinates': {'latitude': 42.345077, 'longitude': -71.063505}, 'transactions': ['pickup', 'restaurant_reservation', 'delivery'], 'price': '$$', 'location': {'address1': '352 Harrison Ave', 'address2': 'Ste B', 'address3': None, 'city': 'Boston', 'zip_code': '02118', 'country': 'US', 'state': 'MA', 'display_address': ['352 Harrison Ave', 'Ste B', 'Boston, MA 02118']}, 'phone': '+16179363282', 'display_phone': '(617) 936-3282', 'distance': 2184.904830516659}, {'id': 'IqyqfQFnBsOIReGrcgaYhA', 'alias': 'avana-sushi-boston', 'name': 'Avana Sushi', 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/y8RWhhblmgUTajfeg1E23g/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/avana-sushi-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 935, 'categories': [{'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'sushi', 'title': 'Sushi Bars'}], 'rating': 4.0, 'coordinates': {'latitude': 42.3515433311481, 'longitude': -71.0609420250346}, 'transactions': ['pickup', 'delivery'], 'price': '$', 'location': {'address1': '42 Beach St', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02111', 'country': 'US', 'state': 'MA', 'display_address': ['42 Beach St', 'Boston, MA 02111']}, 'phone': '+16178187782', 'display_phone': '(617) 818-7782', 'distance': 2659.236805054838}, {'id': 'H8_9t4Tlw2bcKW9qdqAM8w', 'alias': 'sushiemon-boston', 'name': 'Sushiemon', 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/aUV8WZ8vRdKpxGmd-C_jDw/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/sushiemon-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 69, 'categories': [{'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'sushi', 'title': 'Sushi Bars'}], 'rating': 4.0, 'coordinates': {'latitude': 42.3452399, 'longitude': -71.08713}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '201 Massachusetts Ave', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02115', 'country': 'US', 'state': 'MA', 'display_address': ['201 Massachusetts Ave', 'Boston, MA 02115']}, 'phone': '+16172361464', 'display_phone': '(617) 236-1464', 'distance': 620.8288219541366}, {'id': 'DJ8L-KDfSJ7ubkqE-FmqPw', 'alias': 'symphony-sushi-boston', 'name': 'Symphony Sushi', 'image_url': 'https://s3-media2.fl.yelpcdn.com/bphoto/iYuhd-jYvITn9zrMJqzidQ/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/symphony-sushi-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 336, 'categories': [{'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'sushi', 'title': 'Sushi Bars'}], 'rating': 3.5, 'coordinates': {'latitude': 42.3417364060879, 'longitude': -71.0868249088526}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '45 Gainsborough St', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02115', 'country': 'US', 'state': 'MA', 'display_address': ['45 Gainsborough St', 'Boston, MA 02115']}, 'phone': '+16172623888', 'display_phone': '(617) 262-3888', 'distance': 293.8261677846339}, {'id': 'qBDpPs3Wg7EmUI5XGeUEdA', 'alias': 'fugakyu-japanese-cuisine-brookline-2', 'name': 'Fugakyu Japanese Cuisine', 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/uOmVKyZgQOgNmKq8LHvCWA/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/fugakyu-japanese-cuisine-brookline-2?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 1141, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}], 'rating': 4.0, 'coordinates': {'latitude': 42.342954, 'longitude': -71.119374642915}, 'transactions': ['pickup', 'delivery'], 'price': '$$$', 'location': {'address1': '1280 Beacon St', 'address2': None, 'address3': '', 'city': 'Brookline', 'zip_code': '02446', 'country': 'US', 'state': 'MA', 'display_address': ['1280 Beacon St', 'Brookline, MA 02446']}, 'phone': '+16177381268', 'display_phone': '(617) 738-1268', 'distance': 2513.845412216948}, {'id': 'bf-V94Y6ABoHz6SX7-N1_A', 'alias': 'oishii-boston-boston', 'name': 'Oishii Boston', 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/G0zsAlvFBjys7gH_jdp4fw/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/oishii-boston-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 685, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}], 'rating': 4.0, 'coordinates': {'latitude': 42.343216559683, 'longitude': -71.066379535192}, 'transactions': [], 'price': '$$$$', 'location': {'address1': '1166 Washington St', 'address2': None, 'address3': '', 'city': 'Boston', 'zip_code': '02118', 'country': 'US', 'state': 'MA', 'display_address': ['1166 Washington St', 'Boston, MA 02118']}, 'phone': '+16174828868', 'display_phone': '(617) 482-8868', 'distance': 1906.2861695076206}, {'id': 'bin8WnikToct6C77oHBJCg', 'alias': 'no-relation-boston-2', 'name': 'No Relation', 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/gdZQjTT5Ve_iJr-egEbPDg/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/no-relation-boston-2?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 13, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}], 'rating': 4.5, 'coordinates': {'latitude': 42.3454022209913, 'longitude': -71.0640801706998}, 'transactions': [], 'location': {'address1': '11 William E Mullins Way', 'address2': '', 'address3': None, 'city': 'Boston', 'zip_code': '02118', 'country': 'US', 'state': 'MA', 'display_address': ['11 William E Mullins Way', 'Boston, MA 02118']}, 'phone': '+16175301772', 'display_phone': '(617) 530-1772', 'distance': 2149.7139805326547}, {'id': 'oBEI6c3hWNUMIRP5oh8Ptw', 'alias': 'laughing-monk-cafe-boston', 'name': 'Laughing Monk Cafe', 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/uFZoKyX3WXzI-wcarGRI_A/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/laughing-monk-cafe-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 175, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'thai', 'title': 'Thai'}, {'alias': 'asianfusion', 'title': 'Asian Fusion'}], 'rating': 4.5, 'coordinates': {'latitude': 42.3340845, 'longitude': -71.1053358}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '737 Huntington Ave', 'address2': None, 'address3': '', 'city': 'Boston', 'zip_code': '02115', 'country': 'US', 'state': 'MA', 'display_address': ['737 Huntington Ave', 'Boston, MA 02115']}, 'phone': '+16172328000', 'display_phone': '(617) 232-8000', 'distance': 1489.4581119012291}, {'id': 'SSmHkP0tZ4TKn_YUOEkVUg', 'alias': 'cafe-sushi-cambridge', 'name': 'Cafe Sushi', 'image_url': 'https://s3-media2.fl.yelpcdn.com/bphoto/bkqv2lHoXzw3O6AwI8ex3w/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/cafe-sushi-cambridge?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 650, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}], 'rating': 4.0, 'coordinates': {'latitude': 42.37077, 'longitude': -71.11355}, 'transactions': [], 'price': '$$', 'location': {'address1': '1105 Massachusetts Ave', 'address2': '', 'address3': '', 'city': 'Cambridge', 'zip_code': '02138', 'country': 'US', 'state': 'MA', 'display_address': ['1105 Massachusetts Ave', 'Cambridge, MA 02138']}, 'phone': '+16174920434', 'display_phone': '(617) 492-0434', 'distance': 3997.352132530565}, {'id': '-rrC8RtPvvhUPcQFHXVDNQ', 'alias': 'chiharu-brookline', 'name': 'Chiharu', 'image_url': 'https://s3-media2.fl.yelpcdn.com/bphoto/L3lD_VBAuxBJ2ST5Nk7-VQ/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/chiharu-brookline?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 108, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'ramen', 'title': 'Ramen'}, {'alias': 'noodles', 'title': 'Noodles'}], 'rating': 4.5, 'coordinates': {'latitude': 42.329694, 'longitude': -71.126876}, 'transactions': [], 'price': '$$', 'location': {'address1': '370 Boylston St', 'address2': None, 'address3': '', 'city': 'Brookline', 'zip_code': '02445', 'country': 'US', 'state': 'MA', 'display_address': ['370 Boylston St', 'Brookline, MA 02445']}, 'phone': '+16174878684', 'display_phone': '(617) 487-8684', 'distance': 3283.192154896656}, {'id': 'SP97o6xotOT4fATH9HDsmQ', 'alias': 'shuns-kitchen-boston-2', 'name': "Shun's Kitchen", 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/MWV7c_qaw7NnalY-R5WR5Q/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/shuns-kitchen-boston-2?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 103, 'categories': [{'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'chinese', 'title': 'Chinese'}, {'alias': 'asianfusion', 'title': 'Asian Fusion'}], 'rating': 4.0, 'coordinates': {'latitude': 42.3419134834615, 'longitude': -71.0797398537397}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '520 Columbus Ave', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02118', 'country': 'US', 'state': 'MA', 'display_address': ['520 Columbus Ave', 'Boston, MA 02118']}, 'phone': '+16174219588', 'display_phone': '(617) 421-9588', 'distance': 805.5346138618265}, {'id': '87f7kR7nTz8WHnmtLM_S6w', 'alias': 'o-ya-boston', 'name': 'O Ya', 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/etn4CDae-9QdHfvSpKnWQA/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/o-ya-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 690, 'categories': [{'alias': 'japanese', 'title': 'Japanese'}], 'rating': 4.5, 'coordinates': {'latitude': 42.3514083135866, 'longitude': -71.056866645813}, 'transactions': [], 'price': '$$$$', 'location': {'address1': '9 East St Pl', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02111', 'country': 'US', 'state': 'MA', 'display_address': ['9 East St Pl', 'Boston, MA 02111']}, 'phone': '+16176549900', 'display_phone': '(617) 654-9900', 'distance': 2948.646824420503}, {'id': 'dBEr7RLilPrgTCM2GxJcKA', 'alias': 'love-art-sushi-boston', 'name': 'Love Art Sushi', 'image_url': 'https://s3-media2.fl.yelpcdn.com/bphoto/ZQTNR9Rp0ENVo3GeTUBeDQ/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/love-art-sushi-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 249, 'categories': [{'alias': 'poke', 'title': 'Poke'}, {'alias': 'bubbletea', 'title': 'Bubble Tea'}], 'rating': 4.0, 'coordinates': {'latitude': 42.34662, 'longitude': -71.08747}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '1 Haviland St', 'address2': '', 'address3': None, 'city': 'Boston', 'zip_code': '02115', 'country': 'US', 'state': 'MA', 'display_address': ['1 Haviland St', 'Boston, MA 02115']}, 'phone': '+16179826953', 'display_phone': '(617) 982-6953', 'distance': 774.8430146771772}, {'id': 'vVF-M6yj3uUljHFZ_WXeiw', 'alias': 'oppa-sushi-boston-2', 'name': 'Oppa Sushi', 'image_url': 'https://s3-media2.fl.yelpcdn.com/bphoto/4561Ili8Zu-yPSZYcAg0-w/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/oppa-sushi-boston-2?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 234, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'seafood', 'title': 'Seafood'}], 'rating': 4.5, 'coordinates': {'latitude': 42.350904, 'longitude': -71.1313152}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '185 Harvard Ave', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02134', 'country': 'US', 'state': 'MA', 'display_address': ['185 Harvard Ave', 'Boston, MA 02134']}, 'phone': '+16172023808', 'display_phone': '(617) 202-3808', 'distance': 3684.5119566283784}, {'id': 'QhO6jvHAsvbM6K8YuMbZtw', 'alias': 'genki-ya-brookline-brookline-2', 'name': 'Genki Ya - Brookline', 'image_url': 'https://s3-media3.fl.yelpcdn.com/bphoto/4d46YjZer0rtSqs7vFEHBQ/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/genki-ya-brookline-brookline-2?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 715, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'asianfusion', 'title': 'Asian Fusion'}], 'rating': 4.0, 'coordinates': {'latitude': 42.344925, 'longitude': -71.126758}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '398 Harvard St', 'address2': '', 'address3': '', 'city': 'Brookline', 'zip_code': '02446', 'country': 'US', 'state': 'MA', 'display_address': ['398 Harvard St', 'Brookline, MA 02446']}, 'phone': '+16172773100', 'display_phone': '(617) 277-3100', 'distance': 3145.4967290247987}, {'id': 'YA2zC4F32g-BwRfQ0KUeaA', 'alias': 'fins-sushi-and-grill-boston', 'name': "Fin's Sushi and Grill", 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/ZaJ3lqIcCRldumbL-lqKNw/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/fins-sushi-and-grill-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 349, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}], 'rating': 4.0, 'coordinates': {'latitude': 42.34957, 'longitude': -71.09527}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '636 Beacon St', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02215', 'country': 'US', 'state': 'MA', 'display_address': ['636 Beacon St', 'Boston, MA 02215']}, 'phone': '+16172678888', 'display_phone': '(617) 267-8888', 'distance': 1205.3543040765446}, {'id': 'baoHJnYFntDUICr10aFj8Q', 'alias': 'sakana-cambridge-2', 'name': 'Sakana', 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/sSq-KQOPF2EqES5QfhBwtw/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/sakana-cambridge-2?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 85, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'soup', 'title': 'Soup'}], 'rating': 4.5, 'coordinates': {'latitude': 42.3693, 'longitude': -71.11082}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '983 Massachusetts Ave', 'address2': None, 'address3': '', 'city': 'Cambridge', 'zip_code': '02138', 'country': 'US', 'state': 'MA', 'display_address': ['983 Massachusetts Ave', 'Cambridge, MA 02138']}, 'phone': '+16177144646', 'display_phone': '(617) 714-4646', 'distance': 3738.0950573921155}, {'id': 'F8Vvh71ULKVbEnTovrt0RA', 'alias': 'wabora-boston', 'name': 'Wabora', 'image_url': 'https://s3-media3.fl.yelpcdn.com/bphoto/rKkd7B4wt7S_1xj0R8OlSg/o.jpg', 'is_closed': False, 'url': 'https://www.yelp.com/biz/wabora-boston?adjust_creative=OW6fAnhGOHIbS4ZiN4eHcQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=OW6fAnhGOHIbS4ZiN4eHcQ', 'review_count': 215, 'categories': [{'alias': 'sushi', 'title': 'Sushi Bars'}, {'alias': 'japanese', 'title': 'Japanese'}, {'alias': 'asianfusion', 'title': 'Asian Fusion'}], 'rating': 3.5, 'coordinates': {'latitude': 42.34928, 'longitude': -71.08272}, 'transactions': ['pickup', 'delivery'], 'price': '$$', 'location': {'address1': '254 Newbury St', 'address2': '', 'address3': '', 'city': 'Boston', 'zip_code': '02116', 'country': 'US', 'state': 'MA', 'display_address': ['254 Newbury St', 'Boston, MA 02116']}, 'phone': '+16174312284', 'display_phone': '(617) 431-2284', 'distance': 1181.7880222597792}], 'total': 456, 'region': {'center': {'longitude': -71.08909606933594, 'latitude': 42.3397560902317}}}
Step 1: read the documentation (http://www.omdbapi.com)
Step 2: get an API key (http://www.omdbapi.com/apikey.aspx)
Step 3: go!
import requests
__API_URL = "http://www.omdbapi.com"
__API_KEY = ""
request_params = {'apikey':__API_KEY, 'type':'movie', 's':'London'}
r = requests.get(__API_URL, params=request_params)
if r.status_code == 200:
r = r.json()
r
{'Search': [{'Title': 'London Has Fallen', 'Year': '2016', 'imdbID': 'tt3300542', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BMTY1ODY2MTgwM15BMl5BanBnXkFtZTgwOTY3Nzc3NzE@._V1_SX300.jpg'}, {'Title': 'An American Werewolf in London', 'Year': '1981', 'imdbID': 'tt0082010', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BNTYzMDk3MzIyNV5BMl5BanBnXkFtZTgwOTM2OTE4MzE@._V1_SX300.jpg'}, {'Title': 'London Boulevard', 'Year': '2010', 'imdbID': 'tt1213648', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BMjA1OTk2NTI4NF5BMl5BanBnXkFtZTcwOTA5NTE5Ng@@._V1_SX300.jpg'}, {'Title': 'London', 'Year': '2005', 'imdbID': 'tt0449061', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BMTc5MzQxNTU3N15BMl5BanBnXkFtZTcwODE2MzIzMQ@@._V1_SX300.jpg'}, {'Title': 'Namastey London', 'Year': '2007', 'imdbID': 'tt0795434', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BM2E3OGZiZjgtMDYzYy00OWIzLTlmZDItOWEwYWE5MjdmMWFjL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyNjQ2MjQ5NzM@._V1_SX300.jpg'}, {'Title': 'Agent Cody Banks 2: Destination London', 'Year': '2004', 'imdbID': 'tt0358349', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BMjE4NDg4MDgxNF5BMl5BanBnXkFtZTcwMTc3NTQyMQ@@._V1_SX300.jpg'}, {'Title': 'The Lodger: A Story of the London Fog', 'Year': '1927', 'imdbID': 'tt0017075', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BMjE4OTg0ODgyNF5BMl5BanBnXkFtZTgwMDY3NTMzMjE@._V1_SX300.jpg'}, {'Title': 'London to Brighton', 'Year': '2006', 'imdbID': 'tt0490166', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BZjMwNmEzMTQtYzNiOS00MTc4LTk0NGMtOTkxYzY0YzI3NTRhXkEyXkFqcGdeQXVyMTMxMTY0OTQ@._V1_SX300.jpg'}, {'Title': 'Those Magnificent Men in Their Flying Machines or How I Flew from London to Paris in 25 hours 11 minutes', 'Year': '1965', 'imdbID': 'tt0059797', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BOTdlMjk5NjQtMmNiNi00OTE3LTg3MjAtNmQ1MzQ3ZDY2ZTU1XkEyXkFqcGdeQXVyNDk3NzU2MTQ@._V1_SX300.jpg'}, {'Title': "101 Dalmatians II: Patch's London Adventure", 'Year': '2002', 'imdbID': 'tt0324941', 'Type': 'movie', 'Poster': 'https://m.media-amazon.com/images/M/MV5BMjA0NDMwOTc3Nl5BMl5BanBnXkFtZTgwNzg3NTAwNzE@._V1_SX300.jpg'}], 'totalResults': '905', 'Response': 'True'}
request_params = {'apikey':__API_KEY, 'i':'tt0082010'}
r = requests.get(__API_URL, params=request_params)
if r.status_code == 200:
r = r.json()
r
{'Title': 'An American Werewolf in London', 'Year': '1981', 'Rated': 'R', 'Released': '21 Aug 1981', 'Runtime': '97 min', 'Genre': 'Comedy, Horror', 'Director': 'John Landis', 'Writer': 'John Landis', 'Actors': 'Joe Belcher, David Naughton, Griffin Dunne, David Schofield', 'Plot': 'Two American college students on a walking tour of Britain are attacked by a werewolf that none of the locals will admit exists.', 'Language': 'English', 'Country': 'UK, USA', 'Awards': 'Won 1 Oscar. Another 2 wins & 3 nominations.', 'Poster': 'https://m.media-amazon.com/images/M/MV5BNTYzMDk3MzIyNV5BMl5BanBnXkFtZTgwOTM2OTE4MzE@._V1_SX300.jpg', 'Ratings': [{'Source': 'Internet Movie Database', 'Value': '7.6/10'}, {'Source': 'Rotten Tomatoes', 'Value': '88%'}, {'Source': 'Metacritic', 'Value': '60/100'}], 'Metascore': '60', 'imdbRating': '7.6', 'imdbVotes': '80,344', 'imdbID': 'tt0082010', 'Type': 'movie', 'DVD': '09 Dec 1997', 'BoxOffice': 'N/A', 'Production': 'Universal Pictures', 'Website': 'http://www.americanwerewolf.com', 'Response': 'True'}