Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output formatting
#1
Hello Helpers,
I made (partly copied) a script that gathers data from a MCP3008.
It loops as often as there are active MCP channels; in this case 0, 1, and 2.
Now the output comes in three separate lines whereas i would like to have them on one line to store in a CSV file together with the date/time.
Is there a way to accomplish this?? Or am i completely on the wrong path.

#Versie 15 jan 2018

# Importeer libraries.
import sys
import time
import csv
import os


# Definieer een array (temp).
# temp = {}
 
# Definieer variabelen.
DY = time.strftime("%Y")
DM = time.strftime("%m")
DD = time.strftime("%d")
TH = time.strftime("%H")
TM = time.strftime("%M")
 
sensorids = ["28-000009aeb158"]
      # Doet de loop zo vaak als aantal sensors
for sensor in range(len(sensorids)):
  tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave")
  tfile.close()   # Sluit het "bestand" nadat we het gelezen hebben.
  temperaturedata =text.split(" ")[21]    # Selecteer het 21ste woord(tellend vanaf 0)
                                  # Let op eventuele '\n'in string.
  temperature = float(temperaturedata[2:])   # Haal 't=' weg en maak een float
  temp[sensor] = temperature / 1000
  print ("sensor", sensor, "=", temp[sensor], "graden.")    #Print resultaat
  waretemp = '%.1f' % temp[0]    #Afronden op 1 achter de komma
 
data = [DY, DM, DD, TH, TM, waretemp]
print (data)
# Sla gegevens op in een bestand (per maand) in /usr/src/DHT_DATA_[YYYY]_[MM].csv
csvfile = "/home/pi/DHT_DATA_" + DY + "_" + DM + ".csv"
 
# Open het csv bestand en schijf door achter de bestaande inhoud.
# with open(csvfile, "a") as output:
# writer = csv.writer(output, delimiter=";", lineterminator='\n')
# writer.writerow(data)
Thanks for advice, rgds Steffen
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting float number output barryjo 2 921 May-04-2023, 02:04 PM
Last Post: barryjo
  Formatting to output file Mark17 2 1,322 Jan-13-2022, 09:06 PM
Last Post: BashBedlam
  Is there a better way of formatting os.uname() output? CodeWolf 6 3,668 Aug-27-2021, 11:41 PM
Last Post: CodeWolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020