Skip to content

Instantly share code, notes, and snippets.

@kmlawson
Created October 14, 2020 18:14
Show Gist options
  • Save kmlawson/6e581341e64cd25fac375c6e771d0ad9 to your computer and use it in GitHub Desktop.
Save kmlawson/6e581341e64cd25fac375c6e771d0ad9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Requires pypinyin, can be installed with pip: https://github.com/mozillazg/python-pinyin
# Thanks to XerCis on CSDN: https://blog.csdn.net/lly1122334/article/details/105380586
# Example: psort file-with-Chinese-words-on-each-line.txt
import sys
from pypinyin import pinyin, Style
from itertools import chain
def to_pinyin(s):
return ''.join(chain.from_iterable(pinyin(s, style=Style.TONE3)))
tosort= []
fileloc=sys.argv[1]
with open(fileloc) as thefile:
for line in thefile:
tosort.append(line.strip())
print(*sorted(tosort,key=to_pinyin), sep="\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment