简体   繁体   English

如何从 python 中的查询结果中删除“小数”

[英]How to remove “Decimal” from the query result in python

I have this code:我有这个代码:

sql = "SELECT AVG(status = 2)*100 AS percentagem FROM voosinfo WHERE Date = %s"
cursor.execute(sql, (user_input,))

records = cursor.fetchone()
print(records)

Python has two ways of formatting objects as strings, one is the str method and one is the repr method. Python有两种将对象格式化为字符串的方式,一种是str方法,一种是repr方法。 The str value is meant to be the human readable representation of the object, while the repr value is meant as a more complete debug representation. str值意味着 object 的人类可读表示,而repr值意味着更完整的调试表示。

When printing a string, it will print the str value.打印字符串时,它将打印str值。 However, the fetchone method likely returns a tuple or a list.但是, fetchone方法可能会返回一个元组或一个列表。 For these types, the str representation will include the repr representation of all the items.对于这些类型, str表示将包括所有项目的repr表示。

It is up to you to format the string, but if you want eg a comma separated list, try格式化字符串由您决定,但如果您想要例如逗号分隔的列表,请尝试

print(', '.join(records))

or if you know you have a single record, try或者,如果您知道自己只有一条记录,请尝试

record, = records
print(record)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM