python拼音怎么写-Python汉字转换成拼音
Python汉字转拼音
Python汉子转拼音可以使用PinYin模块
使用实例如下:
from pinyin import PinYin
test = PinYin()
test.load_word()
print(test.hanzi2pinyin(string='钓鱼岛是中国的'))
print(test.hanzi2pinyin_split(string='钓鱼岛是中国的', split="-"))
1
2
3
4
5
6
frompinyinimportPinYin
test=PinYin()
test.load_word()
print(test.hanzi2pinyin(string='钓鱼岛是中国的'))
print(test.hanzi2pinyin_split(string='钓鱼岛是中国的',split="-"))
输出:
['diao', 'yu', 'dao', 'shi', 'zhong', 'guo', 'de']
'diao-yu-dao-shi-zhong-guo-de'
1
2
['diao','yu','dao','shi','zhong','guo','de']
'diao-yu-dao-shi-zhong-guo-de'
代码优化
其中hanzi2pinyin函数返回值是一个列表,而hanzi2pinyin_split函数在split参数为空时返回列表,不为空是返回字符串.
但程序存在两个问题,第一是当中文中夹带英文时,英文会丢失.第二则是hanzi2pinyin_split的返回值一会是列表,一会是字符串,让人比较迷糊.
例如:
test.hanzi2pinyin_split(string='钓鱼岛是中国的code123', split="")
1
test.hanzi2pinyin_split(string='钓鱼岛是中国的code123',split="")
我们期待的结果是:
u'diaoyudaoshizhongguodecode123'
1
u'diaoyudaoshizhongguodecode123'
但实际结果为:
u'diaoyudaoshizhongguode'
1
u'diaoyudaoshizhongguode'
为此,在原来的程序中做了如下改写.
1.hanzi2pinyin函数修改
原来的hanzi2pinyin函数:
def hanzi2pinyin(self, string=""):
result = []
if not isinstance(string, unicode):
string = string.decode("utf-8")
for char in string:
key = '%X' % ord(char)
result.append(self.word_dict.get(key, char).split()[0][:-1].lower())
return result
1
2
3
4
5
6
7
8
9
10
defhanzi2pinyin(self,string=""):
result=[]
ifnotisinstance(string,unicode):
string=string.decode("utf-8")
forcharinstring:
key='%X'%ord(char)
result.append(self.word_dict.get(key,char).split()[0][:-1].lower())
returnresult
修改后的hanzi2pinyin函数:
def hanzi2pinyin(self, string=""):
result = []
if not isinstance(string, unicode):
string = string.decode("utf-8")
for char in string:
key = '%X' % ord(char)
if not self.word_dict.get(key):
result.append(char)
else:
result.append(self.word_dict.get(key, char).split()[0][:-1].lower())
return result
1
2
3
4
5
6
7
8
9
10
11
12
13
defhanzi2pinyin(self,string=""):
result=[]
ifnotisinstance(string,unicode):
string=string.decode("utf-8")
forcharinstring:
key='%X'%ord(char)
ifnotself.word_dict.get(key):
result.append(char)
else:
result.append(self.word_dict.get(key,char).split()[0][:-1].lower())
returnresult
修改后的hanzi2pinyin函数可以避免中英文混合的情况下,英文丢失.
2.hanzi2pinyin_split函数修改,将返回值为统一为字符串
原来的hanzi2pinyin_split函数:
def hanzi2pinyin_split(self, string="", split=""):
result = self.hanzi2pinyin(string=string)
if split == "":
return result
else:
return split.join(result)
1
2
3
4
5
6
defhanzi2pinyin_split(self,string="",split=""):
result=self.hanzi2pinyin(string=string)
ifsplit=="":
returnresult
else:
returnsplit.join(result)
修改后的hanzi2pinyin_split函数(不论split参数是否为空,hanzi2pinyin_split均返回字符串):
def hanzi2pinyin_split(self, string="", split=""):
result = self.hanzi2pinyin(string=string)
#if split == "":
# return result
#else:
return split.join(result)
1
2
3
4
5
6
defhanzi2pinyin_split(self,string="",split=""):
result=self.hanzi2pinyin(string=string)
#if split == "":
# return result
#else:
returnsplit.join(result)
python拼音怎么写-Python汉字转换成拼音相关推荐
- java汉字转换成拼音_Java代码将汉字转换成拼音
package com.sucre; //导入的jar包 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pi ...
- 汉字转拼音 python_python实现将汉字转换成汉语拼音的库
本文实例讲述了python实现将汉字转换成汉语拼音的库.分享给大家供大家参考.具体分析如下: 下面的这个python库可以很容易的将汉字转换成拼音,其中用到了一个word.data 的字典,可点击此处 ...
- 汉字转拼音 python_python把汉字转换成拼音实现程序
python把汉字转换成拼音实现程序 文章一个简洁干的汉字转拼音 程序,复制下载就可以直接使用,有需要的同学可以参考一下下. #coding:utf-8 #基于python2.6 table = 'a ...
- javascript汉字转换成拼音
javascript汉字转换成拼音 分类 JavaScript 2007-9-27 7:16:38 浏览 13791 回复 0 隐藏侧边栏 转自 www.div-css.com . 阅读 编辑 运行 ...
- hive 转拼音udf_自定义UDF函数:将汉字转换成拼音
工作需求要讲汉字转换成拼音,自定义UDF函数 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...
- C#下汉字转换成拼音
因为一个开发需求需要把汉字转换成拼音,没想到还真的可以! 找了一个类,是别人整理的,默认情况下第一个字母是大写的.我这边需要都是小写的,可以按照自己情况进行实际的需求进行修改,比如有些时候只要首字母就 ...
- 自定义UDF函数:将汉字转换成拼音
工作需求要讲汉字转换成拼音,自定义UDF函数 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...
- 将中文汉字转换成拼音(全拼)
using System.Globalization; using System.Text; using System.Text.RegularExpressions;/// <summary& ...
- 关于xamarin汉字转换成拼音
c# 中关于汉字转换成拼音 现在虽然是xamarin.from 但是大多是代码还是c#的.在点击title排序时候发现汉字不能正确排序.最后我是把汉字取首字母排序的. 发现一篇很有用的文章:http: ...
- java如何把汉字转换成拼音
如何把汉字转换成拼音,听上去好像有难度,但是网上搜了一下,发现有这方面的工具包,叫做pin4j,怎么用呢? /*** * @param zhongwen 传入的中文字符串* @return 返回转换成 ...
最新文章
- Android runOnUiThread() 方法的使用
- 如何配置Spring Boot Tomcat
- AI:2020年6月22日北京智源大会演讲分享之认知神经基础专题论坛——13:40-14:20毕彦超教授《Knowledge representation in the Human brain》
- vconsole插件_移动端调试面板插件vconsole
- 学习响应式BootStrap来写融职教育网站,Bootsrtap第八天轮播图js特效
- 基础野:细说原码、反码和补码
- 第4章 Python 数字图像处理(DIP) - 频率域滤波3 - 取样和取样函数的傅里叶变换、混叠
- [vue] 说说你对MVC、MVP、MVVM模式的理解
- python程序设计方法_Python程序设计现代方法
- js 跳转到 百度指定地址定位点
- 解除ASP.net上传文件大小的限制
- 程序员如何和产品经理优雅的干架
- 用什么软件可以检测苹果耳机芯片_苹果现在要用 AirPods,彻底占据你的耳朵了...
- 【FFmpeg4.1.4】音视频分离器
- Excel 常用函数——关联匹配函数
- vivado 如何添加时序约束
- uos应用_UOS指标的应用详解
- 深圳垃圾分类告诉你这些都是什么垃圾 不仅要会分还要分的准确才行
- IPv6的被请求节点的组播地址
- 新鲜出炉的 MVVM 脚手架——KtArmor-MVVM