Created
October 14, 2020 18:14
-
-
Save kmlawson/6e581341e64cd25fac375c6e771d0ad9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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