An unexplainable error in .format statement - but only in a larger piece of code? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: An unexplainable error in .format statement - but only in a larger piece of code? (/thread-40672.html) |
An unexplainable error in .format statement - but only in a larger piece of code? - ToniE - Sep-05-2023 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) 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) 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: 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. RE: An unexplainable error in .format statement - but only in a larger piece of code? - deanhystad - Sep-05-2023 The leading double quote character is wrong. Cannot see this in the post, because the post converts the character to a ascii double quote, but you can see it in the file. RE: An unexplainable error in .format statement - but only in a larger piece of code? - ToniE - Sep-05-2023 (Sep-05-2023, 12:19 PM)deanhystad Wrote: The leading double quote character is wrong. Cannot see this in the post, because the post converts the character to a ascii double quote, but you can see it in the file. Many THANKS !!!! good eyes !!! RE: An unexplainable error in .format statement - but only in a larger piece of code? - deanhystad - Sep-05-2023 It is obvious when you stop trying to overanalyze. Think simple. The output showed the bytes where you expect to see ". Where is the "? I knew what I was going to see before I opened the file. RE: An unexplainable error in .format statement - but only in a larger piece of code? - ToniE - Sep-05-2023 (Sep-05-2023, 12:34 PM)deanhystad Wrote: It is obvious when you stop trying to overanalyze. Think simple. The output showed the bytes where you expect to see ". Where is the "? I knew what I was going to see before I opened the file. You must have had a bit of previous knowledge: that \xe2\x80\x9c is the Figure Dash in UTF-8 - I noticed that the quotation mark was gone, but never figured why it became now three bytes... And being 68 makes one sometimes sees a bit less sharp... Anyway thanks a lot ... this has cost me some hours. Debugging an apache script is time consuming always through the error.log. |