Skip to main content
 首页 » 编程设计

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

2025年05月04日8mayingbao

我正在运行 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 .

.