2

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

4

2 回答 2

1

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

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

-查看更智能的字符串转换

.

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请参见这个问题

.

于 2012-10-16T00:37:10.713 回答
0

谢谢你让我走上正轨!最后,我只是在 ObjectListView.py 源代码中添加了两行。在第 3601 行附近,我将 _StringToValue 函数更改为包括:

    if converter == "%cd": ##New Line
        return locale.format("%d", value, grouping=True) ##New Line
    else:
     fmt = converter or "%s"
     try:
        return fmt % value
     except UnicodeError:
        return unicode(fmt) % value

本地导入后还需要设置语言环境,如上图:

  locale.setlocale(locale.LC_ALL, 'en_US')

谢谢,我一直在敲这个头一段时间了。干杯!

于 2012-10-16T17:00:23.147 回答