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

标签 python sorting csv integer objectlistview

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

相关文章:

python - Openstack:如何在python API中获取客户端的IP

python - 找到双线;更快的方法

来自第二个表的 MySQL CSV 导出 ID

database - 如何在 CSV 中表示 EAV?

Python 理解调度程序

python - 使用 nltk pos 标记器时出现错误的 zip 文件错误

c++ - std::map 插入顺序和性能

delphi - 向 TList 和 TStringList 添加稳定排序的简单方法

node.js - Fast-CSV 异步流转换

python - 如何在保留空行的同时拆分?