title: How do I repeatedly call an API using IDs from a CSV and write the output to a new CSV?
body: <p>I'm trying to pull song data for a hundred different songs from the Echonest API. I have the IDs for each song in a CSV file - I'm trying to write a script that reads the IDs, appends them to the API url, and writes the data to a new CSV, but I'm having a little trouble.</p>
  
  <p>Is there a good way to pull the ID codes and append them to the URL in a loop? This is what I have so far; not sure how/where to put it in the part about adding the IDs to the URL.</p>
  
  <pre><code>import urllib2
  import json
  import csv
  from time import sleep
  outfile_path='/Users/path/to/file.csv'
  api_url = 'http://developer.echonest.com/api/v4/song/profile?'
  API_KEY = ''
  writer = csv.writer(open(outfile_path))
  with open('/Users/path/to/file.csv') as f:
      for row in csv.DictReader(f):
          song_id = row['id']
          qs = urllib.urlencode({"api_key": API_KEY,
                                 "bucket": "audio_summary",
                                 "id": song_id})
          url = '{}?{}'.format(API_URL, qs)
          parsed_json = json.load(resource)
          for song in parsed_json['results']:
            row = []
            writer.writerow({k: v.encode('utf-8') for k, v in song.items()})
            sleep(5)
  </code></pre>