方法一:
# pip install pypinyin from pypinyin import pinyin, lazy_pinyin, Style print(lazy_pinyin('中心') ) print(lazy_pinyin('嗯')) print(pinyin('中心', style=Style.FIRST_LETTER)) print(type(pinyin('中心', style=Style.FIRST_LETTER)))
方法二(经测试,下面这种准确度更高):
#pip install xpinyin from xpinyin import Pinyin #将中文转英文 def ChineseToEnglish(chinese): test = Pinyin() english = ''.join(test.get_pinyin(chinese).split('-')) print(english) return english
# pip install pypinyin from pypinyin import pinyin, lazy_pinyin, Style print(lazy_pinyin('中心') ) print(lazy_pinyin('嗯')) print(pinyin('中心', style=Style.FIRST_LETTER)) print(type(pinyin('中心', style=Style.FIRST_LETTER)))