扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
#!/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)
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者