Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do i continuously add data to a .csv file in python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could clear internal buffer of the file with flush:

rf.flush()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading