我正在运行Python 2.7,我有ObjectListView
显示大量的人口统计信息。我可以让它正确排序,但输出以100000.0
格式显示。当我使用locale模块将整数转换为字符串时,它会对降序字符串进行排序,如9,181, 9,069, 818, 813, 8,730,
等。任何想法如何排序整数,但显示输出为逗号格式在ObjectListView
?< / p>
答案 0 :(得分:1)
您可以使用以下内容按整数值而不是显示字符串排序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。
答案 1 :(得分: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')
谢谢你,我现在已经在这个问题上好好打了一会儿。 干杯!