python获取汉字拼音查询翻译器_Python 返回汉字的汉语拼音
后来想到自己Delphi有一个获得拼音的代码。于是找了出来。研究了一下代码如下:
复制代码 代码如下:
function get_hz_pywb(hzstr: string; pytype: integer): string;
var
I: Integer;
allstr: string;
hh: THandle;
pp: pointer;
ss: TStringList;
function retturn_wbpy(tempstr: string; tqtype: integer): string;
var
outstr, str: string;
i: integer;
begin
//################### 汉字查询电位
i := 0;
while i <= ss.Count - 1 do
begin
str := ss.Strings[i];
if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then
begin
str := ss.Strings[i];
Break;
end;
i := i + 1;
end;
//###################
outstr := ''; //提取编码
if tqtype = 1 then
begin
for i := pos('①', str) + 2 to pos('②', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 2 then
begin
for i := pos('②', str) + 2 to pos('③', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 3 then
begin
for i := pos('③', str) + 2 to pos('④', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 4 then
begin
for i := pos('④', str) + 2 to length(str) do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
Result := trim(outstr);
end;
begin
//加载资源文件,将内容赋值给 s
ss := TStringList.Create;
hh := FindResource(hInstance, 'mywb', 'TXT');
hh := LoadResource(hInstance, hh);
pp := LockResource(hh);
ss.Text := pchar(pp);
UnLockResource(hh);
FreeResource(hh);
allstr := '';
i := 0;
while i <= length(hzstr) do //提取汉字字符
begin
if (Ord(hzstr[I]) > 127) then
begin
if allstr = '' then
allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype)
else
allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype);
i := i + 2;
end
else
begin
if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I];
i := i + 1;
end;
end;
ss.Free;
Result := trim(allstr);
en
function get_hz_pywb(hzstr: string; pytype: integer): string;
var
I: Integer;
allstr: string;
hh: THandle;
pp: pointer;
ss: TStringList;
function retturn_wbpy(tempstr: string; tqtype: integer): string;
var
outstr, str: string;
i: integer;
begin
//################### 汉字查询电位
i := 0;
while i <= ss.Count - 1 do
begin
str := ss.Strings[i];
if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then
begin
str := ss.Strings[i];
Break;
end;
i := i + 1;
end;
//###################
outstr := ''; //提取编码
if tqtype = 1 then
begin
for i := pos('①', str) + 2 to pos('②', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 2 then
begin
for i := pos('②', str) + 2 to pos('③', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 3 then
begin
for i := pos('③', str) + 2 to pos('④', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 4 then
begin
for i := pos('④', str) + 2 to length(str) do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
Result := trim(outstr);
end;
begin
//加载资源文件,将内容赋值给 s
ss := TStringList.Create;
hh := FindResource(hInstance, 'mywb', 'TXT');
hh := LoadResource(hInstance, hh);
pp := LockResource(hh);
ss.Text := pchar(pp);
UnLockResource(hh);
FreeResource(hh);
allstr := '';
i := 0;
while i <= length(hzstr) do //提取汉字字符
begin
if (Ord(hzstr[I]) > 127) then
begin
if allstr = '' then
allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype)
else
allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype);
i := i + 2;
end
else
begin
if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I];
i := i + 1;
end;
end;
ss.Free;
Result := trim(allstr);
en
这里需要用到一个资源文件。随后我会把资源文件地址发上来。
我把他改成Python代码供大家研究。。。。
复制代码 代码如下:
# -*-coding:utf-8-*-
# 返回汉字的拼音
def Return_pinyin(word):
global reslist
for line in reslist:
if (word==line[0]+line[1]) or (word==line[2]+line[3]):
str = line
break
# 取①和②之间的内容
s = str.find(u'①')+4
e = str.find(u'②')+3
return str[s:e]
def GetPy(word):
#首先装载资源文件
i=0
allstr = ''
while i
if ord(word[i])>127:
if allstr:
allstr += Return_pinyin(word[i]+word[i+1])
else:
allstr = Return_pinyin(word[i]+word[i+1])
i +=2
else:
if allstr:
allstr += word[i]
else:
allstr = word[i]
i +=1
return allstr
if __name__=='__main__':
f = open('wbtext1.txt','r')
reslist = f.readlines()
f.close()
word = raw_input(u'请输入汉字: ')
print GetPy(word).lower()
如果大家有什么问题欢迎讨论。。。。。。
本文标题: Python 返回汉字的汉语拼音
本文地址: http://www.cppcns.com/jiaoben/python/46737.html
python获取汉字拼音查询翻译器_Python 返回汉字的汉语拼音相关推荐
- python获取汉字拼音查询翻译器_python获取汉字的拼音
#coding:utf-8 #基于python2.6 table = 'a,-20319;ai,-20317;an,-20304;ang,-20295;ao,-20292;ba,-20283;bai, ...
- python汉字拼音查询_python获取一组汉字拼音首字母的方法
本文实例讲述了python获取一组汉字拼音首字母的方法.分享给大家供大家参考.具体实现方法如下: #!/usr/bin/env python # -*- coding: utf-8 -*- def m ...
- python汉字拼音首字母_python获_取一组汉字拼音首字母的方法
python获_取一组汉字拼音首字母的方法 发布时间:2017-09-28 22:10 来源:互联网 当前栏目:web技术类 本文实例讲述了python获取一组汉字拼音首字母的方法.分享给大家供大家参 ...
- python 获取用户的一个输入值_Python中,用于获取用户输入的命令为:
[多选题]以下关于机器学习说法正确的是? [判断题]Python内置函数sum____用来返回数值型序列中所有元素之和. [单选题]关于自定义函数的下列说法不正确的是: [判断题]Python内置函数 ...
- python获取系统时间为字符串_python怎么获取系统当前的时间
python获取系统当前的时间的方法:可以利用datetime.datetime.now()函数来获取.具体方法:1.导入datetime包:2.获取当前的时间,如[curr_time = datet ...
- python获取视频帧的时间_Python提取视频中图片的示例(按帧、按秒)
一.按帧提取 #coding=utf-8 import os import cv2 def save_img(): #提取视频中图片 按照每帧提取 video_path = r'D:\\test\\' ...
- python获取指定单元格内容_python读取excel表格指定位置的内容
今天是第一次写博客,对之前学以致用的内容做一些总结,以备日后忘了或者可以给别人提供一些帮助.话不多说,开始写内容. python读取excel表格指定位置的内容 需求:现在有一个excel表格,里面有 ...
- python获取当前系统的日期_python怎么获取当前系统时间
python获取当前系统时间,包括年月日,时分秒,主要通过Python中的datetime模块来实现. 下面我们就通过具体的代码示例,给大家详细介绍Python获取当前时间日期的实现方法. 代码示例如 ...
- python 获取路径的盘符_python获取磁盘号下盘符步骤详解
这次主要教的是如何通过Python 获取Windows系统下的所有的磁盘盘符,以列表的形式展示出来,获取磁盘号下的盘符包括能够获取到我们正在插在电脑上的U盘,也可以读取到,希望能够对你们在学习过程中有 ...
最新文章
- 数学的关键是概念而非技巧
- CSS如何正确显示人民币符号¥
- 请求成功得到返回数据还是走到catch_面试:SpringMVC在接收到请求后的调用细节是什么?...
- python 理解Matplotlib 3D (三维图) 绘图函数 plot_surface 的 rstride 和 cstride参数
- libevent事件驱动库的学习视频教程
- linux压缩和打包的区别,Linux中的压缩和打包
- LeetCode 1771. 由子序列构造的最长回文串的长度(最长回文子序)
- CSS 文字垂直居中自适应 - 代码篇
- flume1.8实现hdfsSink整点滚动文件
- fastjson map转json_Java对象转JSON咋这么头疼?不!那是你还没使用Fastjson
- TortoiseSVN 配合 Beyond Compare 3 或 WinMerge 的设置
- innodb下的mvcc_InnoDB的MVCC实现原理
- 芝加哥计算机科学硕士录取难度,美国cs研究生申请难度有多大?
- CrystalReport runtime的下载地址
- 深度学习论文翻译解析(二十):YOLOv4: Optimal Speed and Accuracy of Object Detection
- a10 amd 安装黑苹果_黑苹果整合版系统U盘镜像Niresh macOS Sierra 10.12.3 支持Intel/AMD......
- maven项目install报错:\target\surefire-reports for the individual test results
- springBoot thymeleaf 属性为空时报错:EL1007E
- 复杂业务下,我们为何选择Akka作为异步通信框架?
- APP登录 技术点与流程全解
热门文章
- vue 项目 支持 ie9 实现文件上传
- 汽车 ECU 简介 软硬件架构 原理 详细总结
- 计网 - DNS 域名解析系统
- 资本/车企持续加码的新赛道,谁将成为本土赢家?
- 启锐5xx和4xx系列打印机驱动ver2.2.0_2020年十一月 三合一彩色喷墨打印机对比及推荐...
- 数据是如何从无线网卡发送出去的?
- 接私活可用的 Springboot + Vue 快速开发框架
- 【入门教程】tmux精简教程
- C++条形码生成器Aspose.BarCode for C++更新至v19.5 | 附下载
- Simditor 上传图片回显图片src base64 换成 图片链接