title: Extract data from json, append using for loop and save as CSV
body: <p>I have extracted <code>id</code>, <code>username</code>, and <code>name</code> for 100 followers for 102 politicians using Tweepy. The data is stored in a JSON file named <code>pol_followers</code>. Now I wish to append <code>id</code> and <code>username</code> and save it as a CSV file using the function below. However, when using the function in the last line <code>append_followers_to_csv(pol_followers, &quot;pol_followers.csv&quot;)</code> I get the error seen at the bottom.</p>
  <pre><code># Structure of pol_followers. The full pol_followers is much longer... 
  print(json.dumps(pol_followers, indent=4, sort_keys=True))  # see json data structure
  
  [
  {
          &quot;data&quot;: [
              {
                  &quot;id&quot;: &quot;1464206217807601666&quot;,
                  &quot;name&quot;: &quot;terry alex&quot;,
                  &quot;username&quot;: &quot;terryal51850644&quot;
              },
              {
                  &quot;id&quot;: &quot;1479032154394968064&quot;,
                  &quot;name&quot;: &quot;Charles Williams&quot;,
                  &quot;username&quot;: &quot;Charles99924770&quot;
              },
              {
                  &quot;id&quot;: &quot;2526015770&quot;,
                  &quot;name&quot;: &quot;LISA P&quot;,
                  &quot;username&quot;: &quot;LISAP0910&quot;
              },
              {
                  &quot;id&quot;: &quot;2957692520&quot;,
                  &quot;name&quot;: &quot;fayaz ahmad&quot;,
                  &quot;username&quot;: &quot;ahmadfayaz202&quot;
              }
          ],
          &quot;meta&quot;: {
              &quot;next_token&quot;: &quot;F6HS7IU5SRGHEZZZ&quot;,
              &quot;result_count&quot;: 100
          }
      },
      {
          &quot;data&quot;: [
              {
                  &quot;id&quot;: &quot;2482703136&quot;,
                  &quot;name&quot;: &quot;HieuVu&quot;,
                  &quot;username&quot;: &quot;sachieuhaihanh&quot;
              },
              {
                  &quot;id&quot;: &quot;580882148&quot;,
                  &quot;name&quot;: &quot;Maxine D. Harmon&quot;,
                  &quot;username&quot;: &quot;maxxximd&quot;
              },
              {
                  &quot;id&quot;: &quot;1478867472841334787&quot;,
                  &quot;name&quot;: &quot;RBPsych1&quot;,
                  &quot;username&quot;: &quot;RBPsych1&quot;
  </code></pre>
  <pre><code># Create file
  csv_follower_file = open(&quot;pol_followers.csv&quot;, &quot;a&quot;, newline=&quot;&quot;, encoding='utf-8')
  csv_follower_writer = csv.writer(csv_follower_file)
  
  # Create headers for the data I want to save. I only want to save these columns in my dataset
  csv_follower_writer.writerow(
      ['id', 'username'])
  csv_follower_file.close()\
  
  
  def append_followers_to_csv(pol_followers, csv_follower_file):
      # A counter variable
      global follower_id, username
      counter = 0
  
      # Open OR create the target CSV file
      csv_follower_file = open(csv_follower_file, &quot;a&quot;, newline=&quot;&quot;, encoding='utf-8')
      csv_follower_writer = csv.writer(csv_follower_file)
  
      for ids in pol_followers['data']:
  
          # 1. follower ID
          follower_id = ids['id']
  
          # 2. follower username
          username = ids['username']
  
          # Assemble all data in a list
          ress = [follower_id, username]
  
          # Append the result to the CSV file
          csv_follower_writer.writerow(ress)
          counter += 1
  
          # When done, close the CSV file
          csvFile.close()
  
      # Print the number of tweets for this iteration
      print(&quot;# of Tweets added from this response: &quot;, counter)
  
  
  append_followers_to_csv(pol_followers, &quot;pol_followers.csv&quot;)  # Save tweet data in a csv file
  </code></pre>
  <pre><code> File &quot;&lt;input&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;&lt;input&gt;&quot;, line 11, in append_followers_to_csv
  TypeError: list indices must be integers or slices, not str
  </code></pre>