
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
pypinyin
Advanced tools
|Build| |GitHubAction| |Coverage| |Pypi version| |PyPI downloads| |DOI|
将汉字转为拼音。可以用于汉字注音、排序、检索(Russian translation_) 。
最初版本的代码参考了 hotoo/pinyin <https://github.com/hotoo/pinyin>__ 的实现。
.. contents::
.. code-block:: bash
pip install pypinyin
.. code-block:: python
>>> from pypinyin import pinyin, lazy_pinyin, Style
>>> pinyin('中心') # or pinyin(['中心']),参数值为列表时表示输入的是已分词后的数据
[['zhōng'], ['xīn']]
>>> pinyin('中心', heteronym=True) # 启用多音字模式
[['zhōng', 'zhòng'], ['xīn']]
>>> pinyin('中心', style=Style.FIRST_LETTER) # 设置拼音风格
[['z'], ['x']]
>>> pinyin('中心', style=Style.TONE2, heteronym=True)
[['zho1ng', 'zho4ng'], ['xi1n']]
>>> pinyin('中心', style=Style.TONE3, heteronym=True)
[['zhong1', 'zhong4'], ['xin1']]
>>> pinyin('中心', style=Style.BOPOMOFO) # 注音风格
[['ㄓㄨㄥ'], ['ㄒㄧㄣ']]
>>> lazy_pinyin('威妥玛拼音', style=Style.WADEGILES)
['wei', "t'o", 'ma', "p'in", 'yin']
>>> lazy_pinyin('中心') # 不考虑多音字的情况
['zhong', 'xin']
>>> lazy_pinyin('战略', v_to_u=True) # 不使用 v 表示 ü
['zhan', 'lüe']
# 使用 5 标识轻声
>>> lazy_pinyin('衣裳', style=Style.TONE3, neutral_tone_with_five=True)
['yi1', 'shang5']
# 变调 nǐ hǎo -> ní hǎo
>>> lazy_pinyin('你好', style=Style.TONE2, tone_sandhi=True)
['ni2', 'ha3o']
注意事项 :
neutral_tone_with_five=True 开启使用 5 标识轻声 )。v 表示 ü (可以通过参数 v_to_u=True 开启使用 ü 代替 v )。文档 <https://pypinyin.readthedocs.io/zh_CN/master/usage.html#handle-no-pinyin>__ )。嗯 的拼音并不是大部分人以为的 en 以及存在既没有声母也没有韵母的拼音,详见下方 FAQ 中的说明。命令行工具:
.. code-block:: console
$ pypinyin 音乐
yīn yuè
$ python -m pypinyin.tools.toneconvert to-tone 'zhong4 xin1'
zhòng xīn
详细文档请访问:https://pypinyin.readthedocs.io/。
项目代码开发方面的问题可以看看 开发文档_ 。
拼音有误? +++++++++++++++++++++++++++++
可以通过下面的方法提高拼音准确性:
文档 <https://pypinyin.readthedocs.io/zh_CN/master/usage.html#custom-dict>__ 。.. code-block:: python
>> from pypinyin import load_phrases_dict, load_single_dict
>> load_phrases_dict({'桔子': [['jú'], ['zǐ']]}) # 增加 "桔子" 词组
>> load_single_dict({ord('还'): 'hái,huán'}) # 调整 "还" 字的拼音顺序或覆盖默认拼音
pypinyin-dict <https://github.com/mozillazg/pypinyin-dict>__ 项目提供的自定义拼音库来纠正结果。.. code-block:: python
# 使用 phrase-pinyin-data 项目中 cc_cedict.txt 文件中的拼音数据优化结果
>>> from pypinyin_dict.phrase_pinyin_data import cc_cedict
>>> cc_cedict.load()
# 使用 pinyin-data 项目中 kXHC1983.txt 文件中的拼音数据优化结果
>>> from pypinyin_dict.pinyin_data import kxhc1983
>>> kxhc1983.load()
.. code-block:: python
>>> # 使用其他分词模块分词,比如 jieba 之类,
>>> #或者基于 phrases_dict.py 里的词语数据使用其他分词算法分词
>>> words = list(jieba.cut('每股24.67美元的确定性协议'))
>>> pinyin(words)
pypinyin-g2pW <https://github.com/mozillazg/pypinyin-g2pW>__ 这个项目。为什么没有 y, w, yu 几个声母? ++++++++++++++++++++++++++++++++++++++++++++
.. code-block:: python
>>> from pypinyin import Style, pinyin
>>> pinyin('下雨天', style=Style.INITIALS)
[['x'], [''], ['t']]
因为根据 《汉语拼音方案》 <http://www.moe.gov.cn/jyb_sjzl/ziliao/A19/195802/t19580201_186000.html>__ ,
y,w,ü (yu) 都不是声母。
声母风格(INITIALS)下,“雨”、“我”、“圆”等汉字返回空字符串,因为根据
`《汉语拼音方案》 <http://www.moe.gov.cn/jyb_sjzl/ziliao/A19/195802/t19580201_186000.html>`__ ,
y,w,ü (yu) 都不是声母,在某些特定韵母无声母时,才加上 y 或 w,而 ü 也有其特定规则。 —— @hotoo
**如果你觉得这个给你带来了麻烦,那么也请小心一些无声母的汉字(如“啊”、“饿”、“按”、“昂”等)。
这时候你也许需要的是首字母风格(FIRST_LETTER)**。 —— @hotoo
参考: `hotoo/pinyin#57 <https://github.com/hotoo/pinyin/issues/57>`__,
`#22 <https://github.com/mozillazg/python-pinyin/pull/22>`__,
`#27 <https://github.com/mozillazg/python-pinyin/issues/27>`__,
`#44 <https://github.com/mozillazg/python-pinyin/issues/44>`__
如果觉得这个行为不是你想要的,就是想把 y 当成声母的话,可以指定 strict=False ,
这个可能会符合你的预期:
.. code-block:: python
>>> from pypinyin import Style, pinyin
>>> pinyin('下雨天', style=Style.INITIALS)
[['x'], [''], ['t']]
>>> pinyin('下雨天', style=Style.INITIALS, strict=False)
[['x'], ['y'], ['t']]
详见 strict 参数的影响_ 。
存在既没有声母也没有韵母的拼音? +++++++++++++++++++++++++++++++++
是的,strict=True 模式下存在极少数既没有声母也没有韵母的拼音。
比如下面这些拼音(来自汉字 嗯、呒、呣、唔)::
ń ńg ňg ǹg ň ǹ m̄ ḿ m̀
尤其需要注意的是 嗯 的所有拼音都既没有声母也没有韵母,呣 的默认拼音既没有声母也没有韵母。
详见 #109_ #259_ #284_ 。
如何将某一风格的拼音转换为其他风格的拼音? ++++++++++++++++++++++++++++++++++++++++++++
可以通过 pypinyin.contrib.tone_convert 模块提供的辅助函数对标准拼音进行转换,得到不同风格的拼音。
比如将 zhōng 转换为 zhong,或者获取拼音中的声母或韵母数据:
.. code-block:: python
>>> from pypinyin.contrib.tone_convert import to_normal, to_tone, to_initials, to_finals
>>> to_normal('zhōng')
'zhong'
>>> to_tone('zhong1')
'zhōng'
>>> to_initials('zhōng')
'zh'
>>> to_finals('zhōng')
'ong'
更多拼音转换的辅助函数,详见 pypinyin.contrib.tone_convert 模块的
文档 <https://pypinyin.readthedocs.io/zh_CN/master/contrib.html#tone-convert>__ 。
如何减少内存占用? ++++++++++++++++++++
如果对拼音的准确性不是特别在意的话,可以通过设置环境变量 PYPINYIN_NO_PHRASES
和 PYPINYIN_NO_DICT_COPY 来节省内存。
详见 文档 <https://pypinyin.readthedocs.io/zh_CN/master/faq.html#no-phrases>__
更多 FAQ 详见文档中的
FAQ <https://pypinyin.readthedocs.io/zh_CN/master/faq.html>__ 部分。
.. _#13 : https://github.com/mozillazg/python-pinyin/issues/113 .. _strict 参数的影响: https://pypinyin.readthedocs.io/zh_CN/master/usage.html#strict
pinyin-data_ 的数据phrase-pinyin-data_ 的数据《汉语拼音方案》 <http://www.moe.gov.cn/jyb_sjzl/ziliao/A19/195802/t19580201_186000.html>__ 的数据hotoo/pinyin__: 汉字拼音转换工具 Node.js/JavaScript 版。mozillazg/go-pinyin__: 汉字拼音转换工具 Go 版。mozillazg/rust-pinyin__: 汉字拼音转换工具 Rust 版。wolfgitpr/cpp-pinyin__: 汉字拼音转换工具 c++ 版。wolfgitpr/csharp-pinyin__: 汉字拼音转换工具 c# 版。__ https://github.com/hotoo/pinyin __ https://github.com/mozillazg/go-pinyin __ https://github.com/mozillazg/rust-pinyin __ https://github.com/wolfgitpr/cpp-pinyin __ https://github.com/wolfgitpr/csharp-pinyin
.. |Build| image:: https://img.shields.io/circleci/project/github/mozillazg/python-pinyin/master.svg :target: https://circleci.com/gh/mozillazg/python-pinyin .. |GitHubAction| image:: https://github.com/mozillazg/python-pinyin/workflows/CI/badge.svg :target: https://github.com/mozillazg/python-pinyin/actions .. |Coverage| image:: https://img.shields.io/coveralls/github/mozillazg/python-pinyin/master.svg :target: https://coveralls.io/github/mozillazg/python-pinyin .. |PyPI version| image:: https://img.shields.io/pypi/v/pypinyin.svg :target: https://pypi.org/project/pypinyin/ .. |DOI| image:: https://zenodo.org/badge/12830126.svg :target: https://zenodo.org/badge/latestdoi/12830126 .. |PyPI downloads| image:: https://img.shields.io/pypi/dm/pypinyin.svg :target: https://pypi.org/project/pypinyin/
.. _Russian translation: https://github.com/mozillazg/python-pinyin/blob/master/README_ru.rst .. _pinyin-data: https://github.com/mozillazg/pinyin-data .. _phrase-pinyin-data: https://github.com/mozillazg/phrase-pinyin-data .. _开发文档: https://pypinyin.readthedocs.io/zh_CN/develop/develop.html .. _#109: https://github.com/mozillazg/python-pinyin/issues/109 .. _#259: https://github.com/mozillazg/python-pinyin/issues/259 .. _#284: https://github.com/mozillazg/python-pinyin/issues/284
FAQs
汉字拼音转换模块/工具.
We found that pypinyin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.