current position:Home>Don't you know more about a python library before the end of 2021?

Don't you know more about a python library before the end of 2021?

2022-01-29 20:23:07 Dream eraser

Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities

This article has participated in  「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .

Write it at the front

In the work , What I fear most is to encounter problems , I don't know how to solve , for example , You want to convert a paragraph of Chinese into Chinese Pinyin , There's no way to start , But after reading today's article , You will become very good at this operation , Only need to consume in the future 1s, The solution immediately jumps into your mind .

open github:github.com/mozillazg/p… This web site , It's just Chinese characters to Pinyin , As python developer , Skillfully find others ( Daniel ) Written Library , For use , When you improve your coding experience , In the open source circle , Wonderful ~

pypinyin python Chinese pinyin conversion tool

The library is very powerful , Basic functions are available , As of today , The author is still updating the source code , So there is no problem with this library ,2022 One year can also be used for one year , Believe in the power of the big man .

pypinyin python Chinese pinyin conversion tool

pinyin Library application scenario

For this library , There are many application scenarios , For example, the storage of files , In many systems, Chinese support is not friendly , Need to be converted to letters , That conversion to Pinyin is a very good solution , And for URL Inside , This is where letters are often needed , More time , When you develop a project , You'll find that , The final solution to many needs may be to convert Chinese into pinyin or English , Then the problem is solved , At that time, I hope you can think of python There are similar libraries in , What we mentioned today is just one of them .

Code time

install pypinyin

This step is for a skilled hand , It's simple , Just use one line of command , But be aware that if you keep downloading , Need to switch to domestic source .

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

pypinyin python Chinese pinyin conversion tool Three lines of code , First show a wave of basic use

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

No problem , The operation results are correct , And the tone ,emmm...., We usually don't need this .

[['mèng'], ['xiǎng'], ['xiàng'], ['pí'], ['cā']]

The official case , I'll post it directly for you , You can feel strong , When you see that polyphonic characters are also supported , The psychology silently said a big man NB.

# Python 3(Python 2  with hands  ' center '  Replace with  u' center '  that will do ):

>>> from pypinyin import pinyin, lazy_pinyin, Style
>>> pinyin(' center ')
[['zhōng'], ['xīn']]
>>> pinyin(' center ', heteronym=True)  #  Enable polyphonic mode 
[['zhōng', 'zhòng'], ['xī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)  #  Phonetic style 
[['ㄓㄨㄥ'], ['ㄒㄧㄣ']]
>>> lazy_pinyin(' center ')  #  Regardless of polyphony 
['zhong', 'xin']

 Copy code 

For a useful library , Documentation is indispensable , Missing this part , Even if your code is wonderful , Then promote and use , In the face of any problem, it will be ruthlessly amplified , People don't usually use it , because == Not at all ==.

Official documents :pypinyin.readthedocs.io/zh_CN/maste…

pypinyin, In this regard, we have done a good job in documentation , Common usage and questions are clearly written , This has also led to , I have nothing to write ( Actually, I'm lazy ). pypinyin python Chinese pinyin conversion tool This explanation

 Match the most correct Pinyin intelligently according to the phrase .
 Support for polyphony .
 Simple traditional support ,  Phonetic support .
 Support many different Pinyin styles .
 Copy code 

Written in the back

Problem solving

  1. Why promote some python library ?

For a developer , The quickest way to solve the problem is just need , A lot of times , We haven't heard of a module , It leads us to build a lot of wheels repeatedly , Wasted time , Wasted energy , And what we do is not necessarily better than what we already have in the open source circle , So stand on the big man's shoulder first , Let the big guy's code shine , When you have enough ability , Building a better wheel , Leave it to houlang programmer .

  1. How new people should learn Python Language

python After understanding the basic grammatical structure of the language , More is copying practice , A lot of copy code , Don't think that if you copy and paste other people's code during the learning period, you won't grow , Quantitative change leads to qualitative change , You don't copy code , It's hard to grow fast , So the initial stage of new students' learning , See more , Copy more , Find more excellent modules , It seems very slow , In fact, it will grow up very quickly .

A lot of times , We can't solve the technical problem , It's not because we're not skilled , But because we don't know what to do

In this article, I hope you can hear from today that there is a tool called pypinyin.


copyright notice
author[Dream eraser],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201292023025737.html

Random recommended