-1

I'm a beginner programmer trying to work with the Yelp API and I've been able to pull the information I need but I can't figure out how to only export a single part of the address into my csv file. This is the code I am working with:

**Convert the JSON string to a dictionary*

business_data = response.json()
c = csv.writer(open('testing.csv', 'a'), lineterminator ='\n')

for biz in business_data['businesses']:

    c.writerow([biz['name'], biz['location'], biz['phone'], biz['url']])

In the last line of code, in the for loop, I want to be able to target a specific element of the 'location' like this:

#c.writerow([biz['name'], biz['address1'], biz['city'], biz['state'], biz['zip_code'], biz['phone'], biz['url']])

on the Yelp website it shows that I can target these specifics but I just can't seem to figure out how with c.writerow()

Yelp shows that I can target these like this:

businesses[x].location.address1 
businesses[x].location.address2 
businesses[x].location.city 
TylerH
  • 20,799
  • 66
  • 75
  • 101
Student
  • 13
  • 3
  • 1
    _In the last line of code, in the for loop, I want to be able to target a specific element of the 'location' like this:_ Well what's wrong with doing that? Which part are you struggling with? – AMC Feb 19 '20 at 18:24

1 Answers1

1

From the response, biz['location'] is a python dictionary meaning it consists of key, value pairs.

You can validate this by printing type(biz['location']). To answer your question, all you need is to call the dict key & write the value into the file.

c.writerow([biz['name'], biz['location']['address1'], biz['location']['city'], biz['location']['state'], biz['location']['zip_code'] biz['phone'], biz['url']])
savithru
  • 11
  • 1