Sep-05-2023, 11:36 AM
I have a larger piece of python code, called from Apache2 on a Raspberry Pi. Python release is 3.11.4 on my Raspberry. The code snippet which creates strange behavior is:
(input data is a tuple of a dict and the result of a MariaDB query)
Please look at the arrow: "<===== generates strange extra characters"
The output of the print statements of this program are (as found in the apache2 error log:
(as you can see, "t" is a timestamp and record[2] a floating point (temperature) value.
But watch the "(hex) e2,80,x9" bytes in front of the answer string - where do these come from ??
About 6 lines above the marked line, there is a similar construct, with more characters and variables which works perfect over and over again)
Is there anyone who can either shed some light on this strange behavior, or tell me how I can debug this more.
As a full reference I added the full code file as a file.
(input data is a tuple of a dict and the result of a MariaDB query)
def jsonify(input_data): l_request=input_data[0] result=input_data[1] first_record=True for record in result.fetchall(): if (l_request['TYP']=="LUFTE"): if record[2]=="ON": oper=40 else: oper=0 t=record[1].strftime('%Y-%m-%d %H:%M:%S') if first_record: match l_request['TYP']: case "LUFTE": answer='[{{"ti":"{:s}","OP":{:d},"t":{:.1f},"h":{:.1f},"l":{:d}}}'.format(t,oper,record[3],record[4],record[5]) case "TEMP": print (t) print (type(t)) print (record[2]) print (type(record[2])) answer='[{{“ti":"{:s}","t":{:.1f}}}'.format(t,record[2]) <====== generates strange extra characters !! print (answer) print (type(answer)) case _: print ("jsonify no valid TYP:") print (l_request) return(1000) first_record=False else: match l_request['TYP']: case "LUFTE": answer=answer+',{{"ti":"{:s}","OP":{:d},"t":{:.1f},"h":{:.1f},"l":{:d}}}'.format(t,oper,record[3],record[4],record[5]) case "TEMP": answer=answer+',{{"ti":"{:s}","t":{:.1f}}}'.format(t,record[2]) case _: print ("jsonify 2 no valid TYP:") print (l_request) return(1000) answer=answer+']' return(answer)
Please look at the arrow: "<===== generates strange extra characters"
The output of the print statements of this program are (as found in the apache2 error log:
(as you can see, "t" is a timestamp and record[2] a floating point (temperature) value.
But watch the "(hex) e2,80,x9" bytes in front of the answer string - where do these come from ??
About 6 lines above the marked line, there is a similar construct, with more characters and variables which works perfect over and over again)
Output:[Tue Sep 05 13:03:40.059054 2023] [wsgi:error] [pid 17430] [client 192.168.1.5:59518] 2023-09-05 12:59:23, referer: http://192.168.1.199/temp.html
[Tue Sep 05 13:03:40.059131 2023] [wsgi:error] [pid 17430] [client 192.168.1.5:59518] <class 'str'>, referer: http://192.168.1.199/temp.html
[Tue Sep 05 13:03:40.059164 2023] [wsgi:error] [pid 17430] [client 192.168.1.5:59518] 22.0, referer: http://192.168.1.199/temp.html
[Tue Sep 05 13:03:40.059192 2023] [wsgi:error] [pid 17430] [client 192.168.1.5:59518] <class 'float'>, referer: http://192.168.1.199/temp.html
[Tue Sep 05 13:03:40.059238 2023] [wsgi:error] [pid 17430] [client 192.168.1.5:59518] [{\xe2\x80\x9cti":"2023-09-05 12:59:23","t":22.0}, referer: http://192.168.1.199/temp.html
[Tue Sep 05 13:03:40.059266 2023] [wsgi:error] [pid 17430] [client 192.168.1.5:59518] <class 'str'>, referer: http://192.168.1.199/temp.html
I also tried the code in a stand-alone program:t="2023-09-03 16:18:31" print (type(t)) record=21.3 print (type(record)) answer='[{{“ti":"{:s}","t":{:.1f}}}'.format(t,record) print(answer) print (type(answer))which produces correct results:
Output:<class 'str'>
<class 'float'>
[{“ti":"2023-09-03 16:18:31","t":21.3}
<class 'str'>
I am aware that possibly some errors in the code elsewhere in the program can cause this, though in my experience this turns into another error once I change the program slightly (like e.g. adding the print statements) - this is not the case.Is there anyone who can either shed some light on this strange behavior, or tell me how I can debug this more.
As a full reference I added the full code file as a file.