python 通过三方库pypinyin 实现中文转拼音

import pypinyin  # pip install pypinyin

word = '你好'
print(pypinyin.pinyin(word, style=pypinyin.NORMAL))

# 返回 [['ni'], ['hao']]

进一步加工 

import pypinyin  # pip install pypinyin

word = "你好"
a = list()
for j in word:
    s = ''
    for i in pypinyin.pinyin(j, style=pypinyin.NORMAL):  # 不带声调(style=pypinyin.NORMAL)
        s += i[0]
    a.append(s)
print(a)

# 返回 ['ni', 'hao']

继续 

import pypinyin

word = ["你好", "python"]
a = list()
for j in word:
    s = ''
    for i in pypinyin.pinyin(j, style=pypinyin.NORMAL):
        s += i[0]
    a.append(s)
print(a)

# 返回 ['nihao', 'python']

如果只需要首字母

import pypinyin

word = ["你好", "python"]
a = list()
for j in word:
    s = ''
    for i in pypinyin.pinyin(j, style=pypinyin.NORMAL):
        s += i[0][0]
    a.append(s)
print(a)

# 返回 ['nh', 'p']

返回首字母字符串:

import pypinyin

word = ["你好", "python"]
a = list()
for j in word:
    s = ''
    for i in pypinyin.pinyin(j, style=pypinyin.NORMAL):
        s += i[0][0]
    a.append(s)
a = ''.join(a)
print(a)

# 返回 'nhp'

你可能感兴趣的:(python,开发语言)