gpt4 book ai didi

python - ObjectListView 使用逗号对整数进行排序和格式化?

转载 作者:太空宇宙 更新时间:2023-11-04 06:20:13 24 4
gpt4 key购买 nike

我正在运行 Python 2.7,并且我有一个显示大量人口统计信息的 ObjectListView。我可以让它正确排序,但输出显示为 100000.0 格式。当我使用 locale 模块将整数转换为字符串时,它对降序字符串进行排序,例如 9,181, 9,069, 818, 813, 8,730, 等。关于如何像整数一样排序的任何想法,但显示输出在 ObjectListView?

中采用逗号格式

最佳答案

您可以使用它们的整数值而不是它们的显示字符串来排序 yourColumn:

yourColumn = ColumnDefn("Title", "center", 100, "title", stringConverter=int_to_string_with_commas)

-- see smarter string conversions .

.

其中 int_to_string_with_commas 是一个将整数转换为字符串(带逗号)的函数:

import locale
locale.setlocale(locale.LC_ALL, 'en_US') # your locale here

def int_to_string_with_commas(value):
return locale.format("%d", value, grouping=True)

有关编写 int_to_string_with_commas 的其他方法,请参阅 this question .

.

关于python - ObjectListView 使用逗号对整数进行排序和格式化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12905651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com