1

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.

my code so far:

#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='"', quoting=csv.QUOTE_MINIMAL)
    flaresw.writerow(['date and time', 'flare size', 'begin time', 'end time', 'current xray flux'])
    
    while y<10:
        
        #request the json data
        headers = {'User-Agent': 'python program grabbing data from the API'}
        response = requests.get("https://services.swpc.noaa.gov/json/goes/primary/xray-flares-latest.json", 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("debug")
        print(stuff)
        #y=y+1 loop limiter

1 Answers1

1

You could clear internal buffer of the file with flush:

rf.flush()
kosciej16
  • 6,294
  • 1
  • 18
  • 29