. csv files
Forums:
Evening, I'm wanting to display readings from my compass on a webpage using Apache2 I've read an article https://www.phidgets.com/education/learn/projects/pi-website-2/ where they store the sensor readings in a .csv file that is then accesed by the webpage. I've attached the python script I'm using to display my readings in terminal using print. Could you please show me how to create a .csv fie using these readings as I'm not sure where in the script I should place this part copied from the article.
while (True): #Write data to file in CSV format with open ('/var/www/html/data.csv','a') as datafile: datafile.write(str(temperatureSensor.getTemperature()) + "\n")
and how to modify it for my compass readings.
Thanks
John
piborg
Fri, 06/24/2022 - 10:12
Permalink
CSV output
In a .csv file entries are split into columns by commas and rows by new lines. CSV stands for comma separated values :)
By convention each value will have its own column in the file and each separate reading for them has its own line. Sometimes a heading line is also added at the top. For example:
As CSV files are human-readable you can open the output file (
/var/www/html/data.csv
) using a text editor to check the values are what you expect them to be.If you just want the single heading reading from your script, then all you need to do is change out the value in the
str
call so it writes this out instead:If you want multiple values then you can chain them together with a comma like this:
You want to insert the new code after all of your values have been calculated, but before any long delays. In your case this is just above the
time.sleep
line at the bottom of yourwhile
loop.On a side-note, your
print
andtime.sleep
lines are indented so that they are part of theelse
block above. I think you intended them to be further to the left so they are only inside thewhile
loop.