Why does python 3 give me a syntax error in the data list for csv writer?

Question:

I’m running python 3.9.2 on a Raspberry Pi and trying to capture data on a bus and write it to a CSV file. The code used to work on earlier versions of python but now it throws up a syntax error on my data list. Here’s the code …

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

bai = ['/usr/bin/ebusctl', 'read', '-c', 'bai', '-f']
f39 = ['/usr/bin/ebusctl', 'read', '-c', 'f39', '-f']
header = ['Date','Time','Flow','Return','Room Temp']

with open('/media/pi/VAILLANT/vaillant_log.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(bai+['FlowTemp', 'temp']))
    return_temp =  float(check_output(bai+['ReturnTemp', 'temp']))
    room_temp =  float(check_output(f39+['RoomTemp'])

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

    with open('/media/pi/VAILLANT/vaillant_log.csv', 'a', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(data)
read_ebus()

… and here’s the error:

 data = [date,time,flow_temp,return_temp,room_temp]
 ^
SyntaxError: invalid syntax

Has the syntax changed for python 3?

Mike

Asked By: Mike

||

Answers:

There is a missing closing parenthesis.

Change this line
room_temp = float(check_output(f39+['RoomTemp'])

to
room_temp = float(check_output(f39+['RoomTemp']))

Answered By: Subhash Prajapati
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.