小编dar*_*aru的帖子

考虑区域设置的排序列表(瑞典排序)

显然PostgreSQL 8.4和Ubuntu 10.04无法处理为瑞典字母排序W和V的更新方式.也就是说,它仍然将它们命名为相同的字母(瑞典订购的旧定义):

  • VB
  • 厕所
  • VD

它应该是(瑞典订购的新定义):

  • VB
  • VD
  • 厕所

我需要为我正在构建的Python/Django网站正确订购.我已经尝试了各种方法来使用*values_list*来命令从Django QuerySet创建的元组列表.但由于它是瑞典语,ä,ä和ö字母需要正确订购.现在我有一种或另一种方式都不是两种方式..

list_of_tuples = [(u'Wa', 1), (u'Vb',2), (u'Wc',3), (u'Vd',4), (u'Öa',5), (u'äa',6), (u'Åa',7)]

print '########## Ordering One ##############'
ordered_list_one = sorted(list_of_tuples, key=lambda t: tuple(t[0].lower()))
for item in ordered_list_one:
    print item[0]

print '########## Ordering Two ##############'
locale.setlocale(locale.LC_ALL, "sv_SE.utf8")
list_of_names = [u'Wa', u'Vb', u'Wc', u'Vd', u'Öa', u'äa', u'Åa']
ordered_list_two = sorted(list_of_names, cmp=locale.strcoll)
for item in ordered_list_two:
    print item
Run Code Online (Sandbox Code Playgroud)

这些例子给出了:

########## Ordering One ##############
Vb
Vd
Wa
Wc
äa
Åa
Öa
########## …
Run Code Online (Sandbox Code Playgroud)

python django postgresql locale

7
推荐指数
1
解决办法
751
查看次数

标签 统计

django ×1

locale ×1

postgresql ×1

python ×1