Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Writing in the front

At work, the most afraid is to encounter problems, do not know how to solve, for example, you want to convert a paragraph of Chinese into Chinese pinyin, do not know how to start, but after reading today’s article, you will become very good at this operation, after the consumption of 1s, the solution immediately jumped to your mind.

Open github: github.com/mozillazg/p… This website, is not the Chinese characters to pinyin bar, as a Python developer, skilled find others (Daniu) written library, use it, when their coding experience is improved, in the feedback circle, wonderful ~

The library is very powerful, basic functions have, until today, the author is still updating the source code, so there is no problem with this library, 2022 can also use a year, I believe the power of the big guy.

Pinyin library application scenarios

For the library, application scenarios, such as file storage, in many system is not friendly to Chinese support, needs to be converted to the letter, that is converted into pinyin is a very good solution, and used in the URL, it is also often need to letters, more often, when you develop a project, you will find that, Many of the requirements may end up being to convert Chinese to pinyin or English, and then the problem will be solved. Hopefully, you will immediately think that there are similar libraries in Python, and this is just one of them.

Encoding time

Install pypinyin

This step is very simple for a skilled user, just need to use a command, but be aware that if the download is not moving, you need to switch to the domestic source.

pip install pypinyin

pip install pypinyin -i pypi.tuna.tsinghua.edu.cn/simple

Three lines of code, showing a wave of basic use

from pypinyin import pinyin
text = pinyin('Dream Eraser')
print(text)
Copy the code

No problem, no problem running results, but also tone, emmm…. We don’t usually use this.

[‘ meng ‘], [‘xiǎng’], [‘ xiang ‘], [‘ PI ‘], [‘cā’]]

Official given the case, I give you directly posted over, can feel strong, when see also support polyphonic word, psychological silently said a big man NB.

# Python 3(Python 2, replace 'center' with u' center '):

>>> from pypinyin import pinyin, lazy_pinyin, Style
>>> pinyin('center')
[['useful not ng'], ['x and n']]
>>> pinyin('center', heteronym=True)  # Enable polyphonic mode
[['useful not ng'.'zhòng'], ['x and n']]
>>> pinyin('center', style=Style.FIRST_LETTER)  # Set pinyin style
[['z'], ['x']]
>>> pinyin('center', style=Style.TONE2, heteronym=True)
[['zho1ng'.'zho4ng'], ['xi1n']]
>>> pinyin('center', style=Style.TONE3, heteronym=True)
[['zhong1'.'zhong4'], ['xin1']]
>>> pinyin('center', style=Style.BOPOMOFO)  Zhuyin style
[['really ㄨ ㄥ'], ['one ㄒ can be']]
>>> lazy_pinyin('center')  # do not consider polyphonic cases
['zhong'.'xin']

Copy the code

For a good library, the document is indispensable, the lack of this part, even if your code is wonderful, that promotion and use, in the face of any problems will be ruthlessly amplified, we generally will not go to use, because == will not use ==.

The official document: pypinyin. Readthedocs. IO/zh_CN/maste…

pypinyinI did a good job of documenting common usage and problems clearly, which resulted in me having nothing to write about (actually being lazy).This suggests that

According to the phrase intelligent match the most correct pinyin. Supports polyphonic characters. Simple traditional support, phonetic support. Support a variety of different pinyin styles.Copy the code

Write in the back

Problem solving

  1. Why promote some Python libraries?

For a developer, just need the fastest to solve the problem is, most of the time, we have not heard of a module, lead us to a lot of repetition of rolling, wasted time, wasted energy, and make not necessarily better than open source circle existing, stood on the shoulder to the bosses, first so that the code of shine, after my ability enough, Build a better wheel for later programmers.

  1. How should newcomers learn Python

Python language to understand the basic grammar structure, more is imitate practice, a large number of copy code, don’t think yourself in the learning phase of copy and paste the code others, there is no growth, quantitative change to qualitative change, you don’t copy the code, it is difficult to grow rapidly, so the early stages of the new study, read, copy, found that more excellent module, seemingly very slowly, It actually grows up very quickly.

A lot of times, we can’t solve technology problems, not because we don’t have good technology, but because we don’t know how to do it

This article hopes you hear today that there is a tool called Pypinyin.