Python formatting bytes in csv writer

I’ve written a script that captures three data elements from a bus every 10 seconds and writes them to a csv file with date and time. The elements are flow_temp, return_temp and flame. The first two are floating point numbers and flame is bytes. The possible values of flame are ‘on’ and ‘off’ and I’m having trouble formatting the output. Here is the code:

from subprocess import check_output
import csv
from datetime import datetime
import threading

cmd = ['/usr/bin/ebusctl', 'read', '-f']
header = ['Date','Time','Flow','Return','Flame']

with open('sandbox.csv', 'w', newline = '') as f:
    writer = csv.writer(f)

def read_ebus():
  threading.Timer(10.0, read_ebus).start()

  date ='%Y-%m-%d')
  time ='%H:%M:%S')
  flow_temp =  float(check_output([*cmd, 'FlowTemp', 'temp']))
  return_temp =  float(check_output([*cmd, 'ReturnTemp', 'temp']))
  flame =  check_output([*cmd, 'Flame'])

  data = [date,time,flow_temp,return_temp,flame]

  with open('sandbox.csv', 'a', newline='') as f:
    writer = csv.writer(f)

And here is the output when flame = on:


I tried defining flame as a string:

flame =  str(check_output([*cmd, 'Flame']), encoding = 'utf-8')

This gives me:




How can I adjust the code so that flame appears as ‘on’ or ‘off’ in the csv file with no spaces between the lines?



>Solution :

In order to fix the issue with the formatting of the ‘flame’ data, you can modify the line where you define the ‘flame’ variable. Instead of using the check_output() function, you can use the communicate() method of the Popen class from the subprocess module. The communicate() method returns a tuple containing the standard output and standard error of the command, as bytes. You can then use the decode() method to convert the bytes to a string and use the strip() method to remove any whitespace characters from the beginning and end of the string. Here is an example:

from subprocess import Popen, PIPE

# ...

flame = Popen([*cmd, 'Flame'], stdout=PIPE, stderr=PIPE).communicate()[0].decode().strip()

You can then use the flame variable directly in the csv.writerow() method without any additional formatting. The resulting output should look like this:


Leave a Reply