Python 只获取字典值,没有来自 Mysql 的键

标签 python mysql dictionary pymysql

在 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/

相关文章:

python - 合并具有不同索引的多个系列,对重复的索引求和

python - Sentry中使用captureMessage时,如何选择不归类为error的类型?

mysql - SQL : splitting a row in two rows

Python dict 如何在一行中创建 key 或更新 key ?

python - 如何按字典中的值对字典列表进行排序?

python - 将字典值转换为列表?

python - 将字符串转换为日期时如何忽略时间?

python - 使用 PyODBC 从 Linux 连接到 SQL Server

mysql - 如何使这些 wp_postmeta MySQL 查询更快?

mysql - 如何根据col值进行选择