Pavel1953
Posts: 8
Joined: Sat Aug 17, 2019 3:50 pm
Location: Roznov pod Radhostem, Czech Republic

No data written in CSV file

Sat Aug 17, 2019 4:31 pm

Hello,
could anybody be so kind and help?
Thank you
Pavel

A) Following is a code that runs and shows printed data when I use print:

Code: Select all

import explorerhat as eh
import time
import sys
from time import sleep, strftime, time
import csv
with open("humidity.csv", "a") as log:
    while True:
        humidity = eh.analog.three.read()
       print("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str(humidity)))
       sleep(1)
Following is a code that should write data in the csv humidity.csv, but it does not work. No data is written (I have verified no data there are in the file "humidity.csv") .

Code: Select all

import explorerhat as eh
import time
import sys
from time import sleep, strftime, time
import csv

with open("humidity.csv", "a") as log:
    while True:
        humidity = eh.analog.three.read()
        log.write("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str(humidity)))
        sleep(1)

MarkDH102
Posts: 347
Joined: Fri Feb 13, 2015 3:18 pm

Re: No data written in CSV file

Sat Aug 17, 2019 5:29 pm

I've never used log before,but my assumption is that it needs to be closed before you can read the file.
Also,it is better to specify an absolute path. Although as you can see the file this doesn't matter so much.
There may also be a flush operation which you can call after each write which will commit the data to the file.
I'm sure someone who knows will chip in soon...

ghp
Posts: 1413
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: No data written in CSV file

Sat Aug 17, 2019 5:40 pm

change the code a bit and ensure file is closed after each write.

Code: Select all

while True:
    with open("humidity.csv", "a+") as log:
        ... read sensor value ...
        d = "{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str(humidity))
        print(d)
        log.write(d)
        sleep(1)    
The "a+" is 'append or create if not available'.

User avatar
scruss
Posts: 2482
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: No data written in CSV file

Sat Aug 17, 2019 5:46 pm

After your log.write line, add:

Code: Select all

        log.flush()
Python I/O is buffered, so if you break out of a with open(…) loop, the data may never get written.

From the file documentation (pydoc file to read it):

Code: Select all

 |  write(...)
 |      write(str) -> None.  Write string str to file.
 |      
 |      Note that due to buffering, flush() or close() may be needed before
 |      the file on disk reflects the data written.
Mind you, what you really want to do is use that csv module you went to the bother of importing. It's the way of managing CSV files. So maybe something like this:

Code: Select all

#!/usr/bin/env python3
import explorerhat as eh
import time
import sys
from time import sleep, strftime, time
import csv

with open("humidity.csv", "a") as log:
    writer = csv.writer(log)
    while True:
        writer.writerow((strftime("%Y-%m-%d %H:%M:%S"), str(humidity)))
        log.flush()
        sleep(1)
Hope that helps! File management is confusing sometimes!
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Pavel1953
Posts: 8
Joined: Sat Aug 17, 2019 3:50 pm
Location: Roznov pod Radhostem, Czech Republic

Re: No data written in CSV file

Sun Aug 18, 2019 8:11 am

Hello,

I just added the line reading the output signal of the sensor (line in green) and it works nicely.
I consider this blog post to be closed (, but as a complete beginner I do not know how to do it).

Thanks a lot!


The code is as follows:
#!/usr/bin/env python3
import explorerhat as eh
import time
import sys
from time import sleep, strftime, time
import csv

with open("humidity.csv", "a") as log:
humidity = eh.analog.three.read()
writer = csv.writer(log)
while True:
writer.writerow((strftime("%Y-%m-%d %H:%M:%S"), str(humidity)))
log.flush()
sleep(1)

gordon77
Posts: 4175
Joined: Sun Aug 05, 2012 3:12 pm

Re: No data written in CSV file

Sun Aug 18, 2019 9:25 am

You can't close a thread, just change the title to start with [SOLVED]...or similar

User avatar
scruss
Posts: 2482
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: No data written in CSV file

Sun Aug 18, 2019 11:53 am

Pavel1953 wrote:
Sun Aug 18, 2019 8:11 am
I just added the line reading the output signal of the sensor (line in green) and it works nicely.
Oops, I forgot to put that back in. I don't have the sensor, so I had a line that created random numbers instead.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Pavel1953
Posts: 8
Joined: Sat Aug 17, 2019 3:50 pm
Location: Roznov pod Radhostem, Czech Republic

Re: No data written in CSV file

Sun Aug 18, 2019 6:48 pm

Than you.

Pavel1953
Posts: 8
Joined: Sat Aug 17, 2019 3:50 pm
Location: Roznov pod Radhostem, Czech Republic

Re: No data written in CSV file [SOLVED]

Sun Aug 18, 2019 6:50 pm

[SOLVED]

Return to “Python”