如何访问按 Python 中的标签构建的数据?
How can I access data structured by labels in Python?
我正在使用 pymysql 练习 python 和 mysql。执行查询后,我得到具有以下结构的结果:
{'Name': 'Company1', 'SALES': 1113.3, 'CURRENCY': 'USD'}
我想使用 'SALES' 值进行操作,如何访问它?
到目前为止我的代码如下:
with connection.cursor() as cursor:
# Revenue by Company (absolute)
sql1 = "select `Company`.`Name`, `Company`.`SALES`, `Company`.`CURRENCY` from `Company` where `Company`.`Name` = 'Company1'"
cursor.execute(sql1)
result1 = cursor.fetchall()
有什么想法吗?
提前致谢!!
由于您正在调用 .fetchall
,result1
是一个字典列表,此列表中的每个元素都是从查询返回的一行。因此,您需要访问每个字典(=行)中的 'SALES'
键:
for row in results1:
print(row['SALES'])
我正在使用 pymysql 练习 python 和 mysql。执行查询后,我得到具有以下结构的结果:
{'Name': 'Company1', 'SALES': 1113.3, 'CURRENCY': 'USD'}
我想使用 'SALES' 值进行操作,如何访问它?
到目前为止我的代码如下:
with connection.cursor() as cursor:
# Revenue by Company (absolute)
sql1 = "select `Company`.`Name`, `Company`.`SALES`, `Company`.`CURRENCY` from `Company` where `Company`.`Name` = 'Company1'"
cursor.execute(sql1)
result1 = cursor.fetchall()
有什么想法吗?
提前致谢!!
由于您正在调用 .fetchall
,result1
是一个字典列表,此列表中的每个元素都是从查询返回的一行。因此,您需要访问每个字典(=行)中的 'SALES'
键:
for row in results1:
print(row['SALES'])