title: how do i continuously add data to a .csv file in python
body: <p>I want to make a database of solar data with python stored in a .csv file and for that, I need to write to a .csv file in a while True loop. but if the loop never ends it doesn't write the data to the CSV file, but if I put a limit on the while loop when it finishes it writes to the file. so I need to figure out a way to write the data while having the loop go on forever.</p>
  <p>my code so far:</p>
  <pre><code>#imports
  
  import csv
  import json
  import requests
  import datetime
  import time as tim 
  
  #varables
  
  x=0
  y=0
  date=0
  date2=0
  
  #loop
  
  with open('recent_flares.csv', 'a+') as rf:\
      
      #write the header
      flaresw = csv.writer(rf, delimiter=',', quotechar='&quot;', quoting=csv.QUOTE_MINIMAL)
      flaresw.writerow(['date and time', 'flare size', 'begin time', 'end time', 'current xray flux'])
      
      while y&lt;10:
          
          #request the json data
          headers = {'User-Agent': 'python program grabbing data from the API'}
          response = requests.get(&quot;https://services.swpc.noaa.gov/json/goes/primary/xray-flares-latest.json&quot;, headers=headers)
          flare = json.loads(response.text)
          
          #sort the json data
          flares = flare[0]
          time = flares['time_tag']
          
          #if it hasnt do nothing
          if date == time:
               date=time
              
          #if it has updated write the current data to the file
          if date != time:
               stuff = [flares['time_tag'], ]
               flaresw.writerow(stuff)
          
          
          tim.sleep(1)
          print(&quot;debug&quot;)
          print(stuff)
          #y=y+1 loop limiter
  </code></pre>