0

I'm using an API to gather data on businesses in an area and am trying to parse out the results. My code converts the JSON into a python dictionary and I was able to print several items from the dictionary together in my code below.

However, I would like instead to append the results to a list that I can eventually output to a csv file. How would I change the following code below from print, to instead append all of these items to a comma separated list?

for item in data['businesses']:
    print (item['name'], item['rating'], item['review_count'], item['location']['address'])

I'm fairly new to python and coding so I appreciate any additional details or explanations you can provide to the simplest approach.

awesoon
  • 32,469
  • 11
  • 74
  • 99
  • 4
    Aside: if you want to output to a csv file, use `csv.writer` or `csv.DictWriter` instead of trying to make the string yourself. That way you won't have to worry about correctly quoting business names which contain commas (e.g. "Lions, Tigers, and Bears Co."), it'll be handled automatically. – DSM Dec 29 '15 at 04:47

1 Answers1

0

I don't know exactly what format you want it to be in, but I suggest looking at this example. You'll have to open the csv file that you wish to append to with the 'a' parameter, which means append. Then do the same for loop that you have already declared, only instead of printing, you'll do a write.

fd = open('example.csv','a')
for item in data['business']:
    fd.write( // insert data you wish to append )
fd.close()

I believe that code should be a good start to what you want.

Community
  • 1
  • 1