【问题标题】:How to remove "Decimal" from the query result [duplicate]如何从查询结果中删除“小数”[重复]
【发布时间】:2016-11-25 10:09:18
【问题描述】:

我使用以下代码从 PostgreSQL 查询中得到结果:

cur = con.cursor()
cur.execute("""SELECT to_char(tt.service_date,'yyyy-mm-01') AS txn_month,
        SUM (tt.customer) AS customer,SUM (tt.tour) AS tour,
        SUM (tt.distancetraveled) AS distancetraveled
    FROM
        tbl_travel as tt
    GROUP BY
        txn_month""")
rows = cur.fetchall()

我的查询结果是这样的:

[('2016-01-01', Decimal('11.0909090909090909'), Decimal('3.7272727272727273'), 58.5354545454545),
 ('2016-02-01', Decimal('11.6666666666666667'), Decimal('4.0000000000000000'), 74.8766666666667)]

我需要删除值前面的“十进制”字符串并得到如下结果:

[('2016-01-01', '11.0909090909090909', '3.7272727272727273','58.5354545454545'),
 ('2016-02-01', '11.6666666666666667', '4.0000000000000000','74.8766666666667')]

【问题讨论】:

标签: python python-3.x


【解决方案1】:

数字以 Python“十进制”格式存储(请参阅:https://docs.python.org/3.4/library/decimal.html

您可以通过映射数据类型将它们转换为普通字符串,一些示例代码可以做到这一点:

from decimal import Decimal

var = [('2016-01-01', Decimal('11.0909090909090909'), Decimal('3.7272727272727273'),
  58.5354545454545),
 ('2016-02-01', Decimal('11.6666666666666667'), Decimal('4.0000000000000000'),
  74.8766666666667)]

var_fixed = []
for row in var:
    var_fixed.append(list(map(str, list(row))))

这使 var_fixed 为:

[['2016-01-01', '11.0909090909090909', '3.7272727272727273', '58.5354545455'],
 ['2016-02-01', '11.6666666666666667', '4.0000000000000000', '74.8766666667']]

【讨论】:

  • 赵启超它给我的结果是这样的:[, ]
  • 啊,我相信这是因为在 python3+ 中,map 函数返回一个迭代器而不是一个列表,尝试如下编辑这一行: var_fixed.append(list(map(str, list(row))) )
  • 很高兴为您提供帮助。就个人而言,虽然我认为下面 sparkandshine 的列表理解答案更优雅一些,但我会在未来推荐这种方法! :)
【解决方案2】:

使用list comprehensions

from decimal import Decimal

lists = [('2016-01-01', Decimal('11.0909090909090909'), Decimal('3.7272727272727273'), 58.5354545454545),
         ('2016-02-01', Decimal('11.6666666666666667'), Decimal('4.0000000000000000'), 74.8766666666667)]

results = [tuple(str(item) for item in t) for t in lists]

print(results)
[('2016-01-01', '11.0909090909090909', '3.7272727272727273', '58.5354545455'), 
 ('2016-02-01', '11.6666666666666667', '4.0000000000000000', '74.8766666667')]

【讨论】:

    【解决方案3】:

    尝试以下方法:

    res = [(item[0], float(item[1]), float(item[2]), item[3]) for item in my_list]
    

    输出:

    >>> res
    [('2016-01-01', 11.090909090909092, 3.7272727272727275, 58.5354545454545), ('2016-02-01', 11.666666666666666, 4.0, 74.8766666666667)]
    

    使用以下获取字符串值并避免因float 精度而导致的更改:

    >>> res = [(item[0], str(item[1]), str(item[2]), str(item[3])) for item in my_list]
    >>> res
    [('2016-01-01', '11.0909090909090909', '3.7272727272727273', '58.5354545455'), ('2016-02-01', '11.6666666666666667', '4.0000000000000000', '74.8766666667')]
    

    【讨论】:

    • 非常感谢
    • 您正在使用str的案例?使用 float 会稍微改变你的数字的精度。
    猜你喜欢
    • 2017-07-02
    • 2022-11-04
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    相关资源
    最近更新 更多