【发布时间】:2019-11-19 12:27:42
【问题描述】:
我正在通过 python 在 mysql 上运行一些查询。读取数据后,我想按百分比对它们进行排序。任何帮助将很高兴appriciated。
我使用的写法是:
def mid_critical(controller,code):
critical = 75
mid_critical = 50
cursor = controller.cursor()
cursor.execute(code) # execute code
with open('report.txt', 'a') as f:
print("\n******************* Mid Critical: **************", file=f)
for r in cursor: #show tables one by one
if str(type(r[5])) == "<class 'decimal.Decimal'>":
percent = r[5] / r[2] * 100
if percent > mid_critical and percent < critical:
print(r[1],"\nOwner:",r[8],"\nValues:",r[5],"out of", r[2] ,"\nPercent used: %d%% \n" %(percent), file=f)
code 是正在运行的查询。
控制器是进行成功通信的凭据。
一旦写入文件是:
POC
Owner: ACE
Values: 45.1 out of 81.5
Percent used: 55%
DESKTOP
Owner: Nan
Values: 231.8 out of 329.2
Percent used: 70%
REGRESSION
Owner: None
Values: 6.6 out of 10.2
Percent used: 64%
为了举例,我只显示了 3,还有数百个。
我正在寻找的输出是
DESKTOP
Owner: Nan
Values: 231.8 out of 329.2
Percent used: 70%
REGRESSION
Owner: None
Values: 6.6 out of 10.2
Percent used: 64%
POC
Owner: ACE
Values: 45.1 out of 81.5
Percent used: 55%
【问题讨论】:
标签: python mysql sorting parsing