其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
将GBK汉字转化为拼音的Python小程序

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

#!/usr/bin/python
# -*- coding: cp936 -*-

#################################
#   Written by caocao           #
#   [email protected]          #
#   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)




相关文章

相关软件