Csvwriter Not Saving Data to File the Moment I Write It

CSVWriter not saving data to file the moment I write it

Use

with open('myfile.csv','wb') as myfile:
wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
for row in rows:
wrtr.writerow([row.field1,row.field2,row.field3])
myfile.flush() # whenever you want

or

myfile = open('myfile.csv','wb')
wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
for row in rows:
wrtr.writerow([row.field1,row.field2,row.field3])
myfile.flush() # whenever you want, and/or
myfile.close() # when you're done.

The nice thing about the first approach is that your file will also be automatically properly closed in case of an Exception.

If you want your file object to be anonymous, then it will only be closed when the program exits. When or whether it is flushed depends on the OS - so it might be never until exit.

Python CSV not writing data to file

It looks like you want to write a subset of columns to a new file. This problem is simpler with DictReader/DictWriter. Note the correct use of open when using Python 3.x. Your attempt was using the Python 2.x way.

import csv

# headers you want in the order you want
headers = ['header1','header5','header6','header7']

with open('write.csv','w',newline='') as csvWriter,open('read.csv',newline='') as csvfile:
writer = csv.DictWriter(csvWriter,fieldnames=headers,extrasaction='ignore')
readCSV = csv.DictReader(csvfile)
writer.writeheader()
for row in readCSV:
writer.writerow(row)

Test data:

header1,header2,header3,header4,header5,header6,header7
1,2,3,4,5,6,7
11,22,33,44,55,66,77

Output:

header1,header5,header6,header7
1,5,6,7
11,55,66,77

Why Python script has to be executed 2 times to get complete output in the CSV file

I was randomly trying many different ways to solve this issue, and interestingly I found that if we define a function for this code, and then call the function, the issue gets resolved.

def create_subfile():

""" now same code as in question"""

create_subfile();

Does anyone know the reason why a piece of code works fine when executed through a function call, and doesn't work when executed directly ?

writing to csv while output keeps being updated

Multiple solutions :

  • Redirect output to file

    python yourscript.py > log.txt
  • Print to file

    with open('log.txt', 'a') as f:
    print (adcdac.read_adc_voltage(1, 0), file=f)
  • Use logging :

    import logging
    logging.basicConfig(filename='log.txt',level=logging.INFO)
    while True:
    logging.info(adcdac.read_adc_voltage(1, 0))
    time.sleep(0.05)

CSVWriter don't allow to write to file

The problem is that

writer.writeAll accept a String[] as input, you are passing a List<String>

changing

for (String j : data) {
//writer.writeAll(data);//error here
}

to

writer.writeAll(data.toArray(new String[data.size()])); will solve the issue.

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

You could clear internal buffer of the file with flush:

rf.flush()

Reading from a sensor and writing to a CSV file

I ended up, following Pranav Hosangadi's advice, handling the sigterm call manually as

import serial
import signal

def signal_handler(signal, frame):
global interrupted
interrupted = True

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
interrupted = False

if __name__ == '__main__':
ser = serial.Serial('COM4')
with open(filename, 'w') as f:
while True:
if ser.in_waiting > 0:
temp = ser.readline()
f.write(temp)

if interrupted:
break


Related Topics



Leave a reply



Submit