Transform GBK Chinese characters into Python applets

xiaoxiao2021-03-06  134

#! / 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 makess" 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 (strout), "", ", 0

While i

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: strlen = 2

StroutKey = String.center (Strin [i: i 2], strough "" "

StroutValue = String.center (Strtemp, Strlingth) ""

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)

转载请注明原文地址:https://www.9cbs.com/read-99838.html

New Post(0)