python-名字按拼音排序-(用pypinyin)

在python中,用pypinyin实现对名字的排序(按从a-z的顺序排序)

利用python中的pypinyin,具有非常强大的功能:

from pypinyin import pinyin
import re

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    f = open(r".\name.txt","r")
    f_result = open(r".\结果.txt","x")
    content = f.readlines()
    # print(content)
    # content.split(' ')
    print(content)
    str = content
    str.sort(key=pinyin)
    f_result.writelines('\n'.join(str))
    f.close()
    f_result.close()

你可能感兴趣的:(Python,python)