Reputation: 584
I'm using a script that collects data from a database. I wanted to refine the script a bit, but ran into trouble.
I'm running a query that I know will ALWAYS return only one row with 3 values in it. I want to unpack those values into separate variables, but only the last seems to actually have some sort of value.
Here's my code:
import pymysql
cnx = pymysql.connect(host=dbHost, database=dbName, user=dbUser, passwd=dbPass)
query = "select id_parameter,unit,shortname from t_list_metpara where shortname='TT'"
with cnx.cursor() as cursor1:
cursor1.execute(query)
row = cursor1.fetchone()
cnx.close()
id_parameter, unit, shortname = row[:3]
print(row)
print(id_parameter, unit, shortname)
print([type(v) for v in [id_parameter, unit, shortname]])
print([row[v] for v in [0, 1, 2]])
and the result:
(1, '°C\r', 'tt')
tt
[<class 'int'>, <class 'str'>, <class 'str'>]
[1, '°C\r', 'tt']
Can anyone explain to my why Python (or the pymysql package) behaves this way, and what I can do to avoid it?
Upvotes: 0
Views: 89
Reputation: 599926
This has nothing to do with pymysql, or even Python itself, and there is nothing wrong with your variables.
\r
means a carriage return character. If you print something followed by a \r
, the cursor position will return to the start of the line; the next character will begin from the start of that line. If you printed each of id_paramter
, unit
and shortname
separately, you would see all the data.
Upvotes: 1