Sort dictionary by key using locale/collation

后端 未结 2 577
梦如初夏
梦如初夏 2021-02-13 19:28

The following code is ignoring the locale and Égypt goes at the end, what\'s wrong?

dict = {\"United States\": \"United States\", \"Spain\" : \"Spain\", \"Englan         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-13 19:32

    Here's a work-around.

    Use unicode's normalization form canonical decomposition http://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms

    # utf-8 <-> unicode is left as exercise to the reader
    egypt = unicodedata.normalize("NFD", egypt)
    
    sorted(['Egypt', 'E\xcc\x81gypt', 'US'])
    ['Egypt', 'E\xcc\x81gypt', 'US']
    

    This doesn't actually take locale into consideration.

    Beyond this, try newer Python (yes I know) or ICU library from Martijn's linked question and respective answers.

提交回复
热议问题