I'm using this code to compare value with their affiliated fieldnames before i store it in the csv-file to check the right column, because everytime when i reboot my machine the order of sensors in the dictionary will be changed autamatically. this why i have to control the the position of value in dict before i store it in csv file, otherwise i will get a mess csv file like the example bellow which provide the value temperatur is 1000 [°C] the luminisity 10 [lux]
import csv
from datetime import datetime
import os
import time
from collections import OrderedDict
from time import sleep
Datum = time.strftime("%Y-%m-%d")
Time = datetime.now().replace(microsecond=0).isoformat()
data = {'Stecker Energy': '2.37', 'Stromzaehler Strom L1 [A]': '0.0', '1OG Ultraviolet': '0.0','Stecker EG Energie [kWh]': '4.3'}
filename = Datum + 'SOF.csv'
if not os.path.isfile(filename):
outfile = open(filename, "w")
lineKey = ""
for x in data.keys():
lineKey+= x + ","
lineKey = 'Time '+ "," + lineKey[:-1]+"\n" # Delete last character and add new line
outfile = open(filename, "a")
outfile.write(lineKey)
outfile.close()
#check first line of fieldnames
with open(filename,'r') as myFile:
reader = csv.DictReader(myFile)
fieldnames = reader.fieldnames
myFile.close()
#Compare the values with their keys (fieldnames)
with open(filename,'a') as outfile:
lineValue = ""
for sensor, value in data.items():
if sensor in data.items()==sensor in fieldnames:
v = data[sensor]
print("v", v)
lineValue+= str(v) + ","
lineValue = Time + "," + lineValue[:-1] + "\n"
print(lineValue)
outfile.write(lineValue)
outfile.close()
The problem is when i check CSV-file i found that value are written in the wrong column which means wrong fieldnames for Examlpe:
Example of this problem in CSV file
CSV Text Example
Time ,Stromzaehler Strom L1 [A],Stecker Energy,1OG Ultraviolet,Stecker EG Energie [kWh] 2017-11-21T17:56:10,2.37,0.0,0.0,4.3 2017-11-21T17:56:26,4.3,0.0,0.0,2.37 2017-11-21T17:56:28,0,0.0,2.37,4.3 2017-11-21T17:56:30,0,2.37,0.0,4.3
data = {'Stecker Energy': '', 'Stromzaehler Strom L1 [A]': '', '1OG Ultraviolet': '','Stecker EG Energie [kWh]': ''}