From b2983c3e319f3b94d65a306c728c8394d74224ee Mon Sep 17 00:00:00 2001 From: Daniel Ashman Date: Thu, 16 Apr 2015 17:34:54 +1200 Subject: [PATCH] add header to csv files and deliminate with commas If the csv file has just been created (no data contained) write header line. Note should really have the temperature units too. Use commas to deliminate instead of semi-colons. Has not been tested. --- brewpi.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/brewpi.py b/brewpi.py index bfea64a..fdbcbef 100644 --- a/brewpi.py +++ b/brewpi.py @@ -773,14 +773,23 @@ def renameTempKey(key): #write csv file too csvFile = open(localCsvFileName, "a") try: - lineToWrite = (time.strftime("%b %d %Y %H:%M:%S;") + - str(newRow['BeerTemp']) + ';' + - str(newRow['BeerSet']) + ';' + - str(newRow['BeerAnn']) + ';' + - str(newRow['FridgeTemp']) + ';' + - str(newRow['FridgeSet']) + ';' + - str(newRow['FridgeAnn']) + ';' + - str(newRow['State']) + ';' + + #is this a new csv file? + #opened with append so is at the end of the file. If tell returns 0 there is no data in the file (new file) + if csvFile.tell() == 0: + #add header + lineToWrite = 'DateTime,BeerTemp,BeerSet,BeerAnn,FridgeTemp,FridgeSet,FridgeAnn,State,RoomTemp\n' + csvFile.write(lineToWrite) + except KeyError, e: + logMessage("KeyError in line from controller: %s" % str(e)) + try: + lineToWrite = (time.strftime("%b %d %Y %H:%M:%S,") + + str(newRow['BeerTemp']) + ',' + + str(newRow['BeerSet']) + ',' + + str(newRow['BeerAnn']) + ',' + + str(newRow['FridgeTemp']) + ',' + + str(newRow['FridgeSet']) + ',' + + str(newRow['FridgeAnn']) + ',' + + str(newRow['State']) + ',' + str(newRow['RoomTemp']) + '\n') csvFile.write(lineToWrite) except KeyError, e: