Skip to main content

汉字拼音转换工具(Python 版)

Build appveyor Coverage PyPI version DOI

将汉字转为拼音。可以用于汉字注音、排序、检索(Russian translation) 。

基于 hotoo/pinyin 开发。

特性

  • 根据词组智能匹配最正确的拼音。

  • 支持多音字。

  • 简单的繁体支持, 注音支持。

  • 支持多种不同拼音/注音风格。

安装

$ pip install pypinyin

使用示例

Python 3(Python 2 下把 '中心' 替换为 u'中心' 即可):

>>> from pypinyin import pinyin, lazy_pinyin, Style
>>> 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.BOPOMOFO)  # 注音风格
[['ㄓㄨㄥ'], ['ㄒㄧㄣ']]
>>> pinyin('中心', style=Style.CYRILLIC)  # 俄语字母风格
[['чжун1'], ['синь1']]
>>> lazy_pinyin('中心')  # 不考虑多音字的情况
['zhong', 'xin']

注意事项 :

  • 拼音结果不会标明哪个韵母是轻声,轻声的韵母没有声调或数字标识。

  • 无声调相关拼音风格下的结果会使用 v 表示 ü 。

命令行工具:

$ pypinyin 音乐
yīn yuè
$ pypinyin -h

文档

详细文档请访问:http://pypinyin.rtfd.io/ 。

项目代码开发方面的问题可以看看 开发文档 。

FAQ

词语中的多音字拼音有误?

目前是通过词组拼音库的方式来解决多音字问题的。如果出现拼音有误的情况, 可以自定义词组拼音来调整词语中的拼音:

>>> from pypinyin import Style, pinyin, load_phrases_dict
>>> pinyin('步履蹒跚')
[['bù'], ['lǚ'], ['mán'], ['shān']]
>>> load_phrases_dict({'步履蹒跚': [['bù'], ['lǚ'], ['pán'], ['shān']]})
>>> pinyin('步履蹒跚')
[['bù'], ['lǚ'], ['pán'], ['shān']]

详见 文档 。

为什么没有 y, w, yu 几个声母?

>>> from pypinyin import Style, pinyin
>>> pinyin('下雨天', style=Style.INITIALS)
[['x'], [''], ['t']]

因为根据 《汉语拼音方案》 , y,w,ü (yu) 都不是声母。

声母风格(INITIALS)下,“雨”、“我”、“圆”等汉字返回空字符串,因为根据 《汉语拼音方案》 , y,w,ü (yu) 都不是声母,在某些特定韵母无声母时,才加上 y 或 w,而 ü 也有其特定规则。 —— @hotoo

如果你觉得这个给你带来了麻烦,那么也请小心一些无声母的汉字(如“啊”、“饿”、“按”、“昂”等)。 这时候你也许需要的是首字母风格(FIRST_LETTER)。 —— @hotoo

参考: hotoo/pinyin#57, #22, #27, #44

如果觉得这个行为不是你想要的,就是想把 y 当成声母的话,可以指定 strict=False , 这个可能会符合你的预期:

>>> from pypinyin import Style, pinyin
>>> pinyin('下雨天', style=Style.INITIALS)
[['x'], [''], ['t']]
>>> pinyin('下雨天', style=Style.INITIALS, strict=False)
[['x'], ['y'], ['t']]

详见 strict 参数的影响 。

如何减少内存占用

如果对拼音的准确性不是特别在意的话,可以通过设置环境变量 PYPINYIN_NO_PHRASES 和 PYPINYIN_NO_DICT_COPY 来节省内存。 详见 文档

更多 FAQ 详见文档中的 FAQ 部分。

拼音数据

Metadata

Release files for pypinyin 0.35.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypinyin 0.35.3
File Size Uploaded
pypinyin-0.35.3.tar.gz 759.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pypinyin 0.35.3
File Interpreter ABI Platform
pypinyin-0.35.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 1.5 MB

Release files / pypinyin-0.35.3.tar.gz

Download URL pypinyin-0.35.3.tar.gz
Size 759.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4cb8fcc5ca5a77da9aa277ba32c9d1c647e76959a28ddf082cb028a95557dcf2
BLAKE2b-256 checksum
How to use checksums
7652148fe0ca031763d159f9e55d0c81e580fa6f0347cf4cc489224508e24867
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release files / pypinyin-0.35.3-py2.py3-none-any.whl

Download URL pypinyin-0.35.3-py2.py3-none-any.whl
Size 770.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
2b047d1ca885735c294282bcb6f09350ee891b023d3c851a9e38876e689b9d66
BLAKE2b-256 checksum
How to use checksums
bb7d2ed2011b6951f59146148a0184d9a239640cb8487a9fa6d16b94bc71244f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release history Release notifications | RSS feed

0.55.0

2 release files

0.54.0

2 release files

0.53.0

2 release files

0.51.0

2 release files

0.50.0

2 release files

0.49.0

2 release files

0.48.0

2 release files

0.47.1

2 release files

0.47.0

2 release files

0.46.0

2 release files

0.45.0

2 release files

0.44.0

2 release files

0.42.1

2 release files

0.42.0

2 release files

0.41.0

2 release files

0.40.0

2 release files

0.39.0

2 release files

0.36.0

2 release files

0.35.4

2 release files

This release

0.35.3 This release

2 release files

0.35.0

2 release files

0.34.1

2 release files

0.33.1

2 release files

0.32.0

2 release files

0.31.0

2 release files

0.30.1

2 release files

0.29.0

2 release files

0.27.0

2 release files

0.26.1

2 release files

0.26.0

2 release files

0.24.0

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.18.2

2 release files

0.18.1

2 release files

0.18.0

0.17.0

2 release files

0.16.1

2 release files

0.16.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

1 release file

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page