python-pinyin: A Python Library for Converting Chinese Characters to Pinyin
python-pinyin is an open‑source Python library that converts Chinese characters into Pinyin with support for multiple styles, heteronym handling, tone marks, Bopomofo, Wade‑Giles, and configurable options for neutral tones and vowel representation.
python-pinyin is an open‑source Python library that converts Chinese characters into Pinyin, offering various output styles such as first‑letter, tone numbers, tone marks, Bopomofo, and Wade‑Giles, and supporting heteronym (multiple‑pronunciation) handling.
Typical usage examples include importing the library and calling pinyin() or lazy_pinyin() with different parameters to control style, heteronym mode, neutral‑tone representation, and vowel notation.
<code>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.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) # ['zhan', 'lüe']
lazy_pinyin('衣裳', style=Style.TONE3, neutral_tone_with_five=True) # ['yi1', 'shang5']
lazy_pinyin('你好', style=Style.TONE2, tone_sandhi=True) # ['ni2', 'ha3o']
</code>By default, the result does not indicate which vowel is a neutral tone; you can enable neutral‑tone marking with neutral_tone_with_five=True to use “5”.
Without tone‑related styles, the library uses v to represent ü ; set v_to_u=True to output the actual ü character.
Characters without a Pinyin mapping are output unchanged; custom handling is described in the documentation.
*Disclaimer: This article is compiled from online sources; copyright belongs to the original author. Contact us for removal or licensing requests.
Click Read Original to learn more.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.