在 Python3 中,我通过字典从 Mysql
中获取值。 (使用 pymysql.cursors.SSDictCursor
)
但是,我将函数 get point and display 写到 html
(Flask),它显示了 dict key & dict
这是我的代码
staffCrt['B'] = repo.countstaff(month['bonus'], ">", line[0])
staffCrt['S'] = repo.countstaff(month['bonus'], "<", line[0])
staffList.append(staffCrt)
countstaff()中的SQL
sql = "SELECT count(point) from matches where month = '12'"
self.cursor.execute(sql)
return self.cursor.fetchone()
函数返回字典:
{'count(point)': 15}
我已经尝试使用了
staffCrt['B'].values()
但它返回 字典值([15])
我怎样才能只得到值“15”?
最佳答案
从字典访问特定值:
基本上,d[key] = value
字典值:
如果您在字典 d
上调用 d.values()
,将返回一个您应该在 中转换的
如果你想访问它的数据。 dict_values
对象>list列表(d.values())
您的问题:
The function return the dict: {'count(point)': 15}
那么,如果 output
是函数的返回值:
output['count(point)']
将返回 15
随着您的编辑:
实际上 output = staffCrt['B']
所以,
staffCrt['B']['count(point)'] = 15
关于Python 只获取字典值,没有来自 Mysql 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54125126/