2
我想修改csvsort.py(csvkit,https://csvkit.readthedocs.org/en/0.9.0/)以正確處理變音符號。python csv與'右'的變音符號排序
我發現,排序列表工作perfecty代碼(http://www.gossamer-threads.com/lists/python/python/1030549):
alphabet = (
u' ', u'.', u'\'', u'-', u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'a', u'A', u'ä', u'Ä', u'á', u'Á', u'â', u'Â',
u'à', u'À', u'å', u'Å', u'b', u'B', u'c', u'C', u'ç', u'Ç', u'd', u'D', u'e', u'E', u'ë', u'Ë', u'é', u'É', u'ê', u'Ê', u'è', u'È',
u'f', u'F', u'g', u'G', u'h', u'H', u'i', u'I', u'ï', u'Ï', u'í', u'Í', u'î', u'Î', u'ì', u'Ì', u'j', u'J', u'k', u'K', u'l', u'L',
u'm', u'M', u'n', u'ñ', u'N', u'Ñ', u'o', u'O', u'ö', u'Ö', u'ó', u'Ó', u'ô', u'Ô', u'ò', u'Ò', u'ø', u'Ø', u'p', u'P', u'q', u'Q',
u'r', u'R', u's', u'S', u't', u'T', u'u', u'U', u'ü', u'Ü', u'ú', u'Ú', u'û', u'Û', u'ù', u'Ù', u'v', u'V', u'w', u'W', u'x', u'X',
u'y', u'Y', u'z', u'Z'
)
hashindex = {character:index for index, character in enumerate(alphabet)}
def string2sortlist(string):
return [hashindex[s] for s in string]
import random
things_to_sort = ["".join(random.sample(alphabet, random.randint(4, 6)))
for _ in range(200000)]
print(things_to_sort[:15])
things_to_sort.sort(key=string2sortlist)
print(things_to_sort[:15])
所以,問題是:
我應該如何修改
sorter = lambda r: [(r[c] is not None, r[c]) for c in column_ids]
rows.sort(key=sorter, reverse=self.args.reverse)
從csvsort。 py,從上面的工作代碼中使用hashindex()。
TIA。
Miguel。
謝謝;我非常接近。現在我可以使用其他方便的函數而不是string2sortlist(),例如strip_accents()(http://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a -python-Unicode的串/ 518232#518232) – rbenit68