将GBK汉字转化为拼音的Python小程序
2005-09-13 10:06
483 查看
.comment {color:#009900}
.keyword {color:#0000FF; font-weight:bold}
.string {color:#999999}
.number {color:#FF0000}
.buildinfunctions {color:#FF0000}
.methods {color:#009999}
.attributes {color:#009999}
.exceptions {color:#FF0000}
.op {color:#993300}
.commonlibs {color:#660066}
.userdefine {color:#0000FF}
.keyword {color:#0000FF; font-weight:bold}
.string {color:#999999}
.number {color:#FF0000}
.buildinfunctions {color:#FF0000}
.methods {color:#009999}
.attributes {color:#009999}
.exceptions {color:#FF0000}
.op {color:#993300}
.commonlibs {color:#660066}
.userdefine {color:#0000FF}
#!/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小程序
- GBK汉字转化为拼音或笔画的PHP类
- 将一个汉字(GBK)转化成拼音
- GBK汉字转化为拼音或笔画的PHP类
- 【ASP】GBK汉字转拼音程序,支持多音字
- python把汉字转换成拼音实现程序
- 汉字转拼音小程序——Python版
- Android 中汉字转化为拼音
- python编程-将Python程序转化为可执行程序
- C#汉字转拼音,自动识别多音字,带声调,提供正向、逆向、双向分词算法的小程序
- 用C#实现汉字转化为拼音
- 汉字转化为拼音
- ASP将汉字转化为拼音
- 汉字转化为拼音
- python汉字转换拼音
- 能把汉字转化为拼音的一个函数
- [编程心得]用Python给汉字加上带音调的拼音
- c#汉字转拼音(GBK,GB2312)