Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert all Chinese names into pinyin with Python in one second

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to convert all Chinese names into pinyin in one second with Python". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Xpinyin

To get to the point, you can use xpinyin to convert Python Chinese characters to Pinyin, and you can install it directly using pip.

Pip install xpinyin-I http://pypi.douban.com/simple-- trusted-host pypi.douban.com

Then import the Pinyin method under xpinyin as follows

From xpinyin import Pinyin

First of all, try Chinese name to Pinyin.

P = Pinyin () result1 = p.get_pinyin ('Leaf Futian') result1

The results are as follows:

'ye-fu-tian'

Tone_marks can display tone.

Result2 = p.get_pinyin ('Ye Futian', tone_marks='marks')

The results are as follows:

'y è-f ú-titled n'

Remove the spaces?

S = result1.split ('-') result3 = s [0] .capitalize () +'+ '.join (s [1:]) .capitalize () result3 results as follows:' Ye Futian'

Sometimes it is very simple to transfer Chinese names to the initials of Pinyin.

II. Pypinyin

Another way is to use pypinyin, which can also be installed using pip

# install pip install pypinyin-I http://pypi.douban.com/simple-- trusted-host pypi.douban.com

Just import it?

Import pypinyin

Let's take a look at the realization of Chinese name to Pinyin.

Result1 = pypinyin.pinyin ('Ye Tingyun', style=pypinyin.NORMAL) result1

The results are as follows:

[['ye'], [' ting'], ['yun']]

Enable multi-syllables to achieve tone

Result2 = pypinyin.pinyin ('Ye Tingyun', heteronym=True) result2

The results are as follows:

[['y è', 'xie'], ['t í ng'], ['yhost n']]

Because a nested list is returned, you need to make a simple adjustment

Result_ = [I [0] for i in result1] result3 = result_ [0] .capitalize () +'+ '.join (result_ [1:]) .capitalize () result3

The results are as follows:

'Ye Tingyun'

When enabling the multi-tone word mode, I unexpectedly found that'Ye'is also a multi-tone word. I inquired about the data and found that this is indeed the case.

The following is the method of transferring Chinese names to the first letters of Pinyin.

III. Snownlp

The last way is to use snownlp, and the same pip installation and import are as follows:

# install pip install snownlp-I http://pypi.douban.com/simple-- trusted-host pypi.douban.com # Import from snownlp import SnowNLP

Chinese name to Pinyin?

S = SnowNLP ('Ye Tingyun') s.pinyin

The results are as follows:

['ye',' ting', 'yun']

Convert list to string

Result4 = s.pinyin [0] .capitalize () +'+ '.join (s.pinyin [1:]) .capitalize () result4

The results are as follows:

'Ye Tingyun'

Finally, it is the initials of Chinese name to Pinyin.

Convert Chinese names into pinyin in batches

Now, after mastering the basic use, we can use batch operation to free our hands.

Import pypinyin import pandas as pd df = pd.read_excel ('student list _ test.xls') df.head ()

Import a list of students as follows

The following is the batch conversion of all students' Chinese names into pinyin using pypinyin

Pinyin_name = [] first_pinyin = [] for i in df ['name']: result = pypinyin.pinyin (I, style=pypinyin.NORMAL) result_ = [I [0] for i in result] result2 = result_ [0] .capitalize () +'+ '.join (result_ [1:]) .capitalize () result3 =' .join ([I [0] .upper () for i in result_]) print (result2, I) Sep='') pinyin_name.append (result2) first_pinyin.append (result3) df ['English name'] = pinyin_name df ['Pinyin initials'] = first_pinyin df.head ()

Or convert it to the first letter of Pinyin, and the running result is as follows:

This is the end of the introduction of "how to convert all Chinese names into pinyin in one second with Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report