Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)
    writer.writerow(header)

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

  date = datetime.today().strftime('%Y-%m-%d')
  time = datetime.today().strftime('%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)
    writer.writerow(data)
read_ebus()

And here is the output when flame = on:

Date,Time,Flow,Return,Flame
2022-12-05,19:55:42,69.0,56.56,b'on\n\n'
2022-12-05,19:55:52,69.0,56.56,b'on\n\n'

I tried defining flame as a string:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

This gives me:

Date,Time,Flow,Return,Flame
2022-12-05,20:05:26,69.0,58.19,"on

"
2022-12-05,20:05:36,69.0,58.19,"on

"

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

Thanks,

Mike

>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:

Date,Time,Flow,Return,Flame
2022-12-05,19:55:42,69.0,56.56,on
2022-12-05,19:55:52,69.0,56.56,on
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading