#!/usr/bin/python # -*- coding: cp936 -*- ################################# # Written by caocao # # caocao@eastday.com # # http://nethermit.yeah.net # ################################# import sys import re import string class CConvert: def __init__(self): "Load data table" try: fp=open("convert.txt") except IOError: print "Can't load data from data.txt/nPlease make sure this file exists." sys.exit(1) else: self.data=fp.read() fp.close() def convert(self, strIn): "Convert GBK to PinYin" length, strOutKey, strOutValue, i=len(strIn), "", "", 0 while i<length: if i==length-1: strOutKey+=strIn[i:i+1]+" " strOutValue+=strIn[i:i+1]+" " break code1, code2=ord(strIn[i:i+1]), ord(strIn[i+1:i+2]) if code1>=0x81 and code1<=0xFE and code2>=0x40 and code2<=0xFE and code2!=0x7F: strTemp=self.getIndex(strIn[i:i+2]) strLength=len(strTemp) if strLength<2:strLength=2 strOutKey+=string.center(strIn[i:i+2], strLength)+" " strOutValue+=string.center(strTemp, strLength)+" " i+=1; else: strOutKey+=strIn[i:i+1]+" " strOutValue+=strIn[i:i+1]+" " i+=1 return [strOutValue, strOutKey] def getIndex(self, strIn): "Convert single GBK to PinYin from index" pos=re.search("^"+strIn+"([0-9a-zA-Z]+)", self.data, re.M) if pos==None: return strIn else: return pos.group(1)
将GBK汉字转化为拼音的Python小程序
最新推荐文章于 2021-10-19 10:32:37 发布
此博客展示了一段Python代码,定义了CConvert类实现GBK到拼音的转换。通过读取convert.txt文件加载数据,在convert方法中对输入字符串逐字符处理,利用getIndex方法查找对应拼音,若未找到则返回原字符。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.9
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
7万+





