惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - 网络隐士

[游戏] 连连看之eBay易趣搞笑版 [.NET之西天取经] (01) 即将踏上去硅谷的班机 张江大厦蒸包子,东湖物业真抠门 淘宝的人工封IP技术真好玩 感叹吴总落户易趣 美国签证二进宫 隐士搞定美国签证惊魂记 eBay之歌(Flash版) 阶乘运算之Python VS Java Python代码高亮显示工具 一个小巧的MySQL Shell 一个小巧的Python Shell 根据CDImage.cue产生自动改名命令的Python小程序 繁体中文转换为简体中文的PHP类 简体中文转换为繁体中文的PHP类 GBK汉字转化为拼音或笔画的PHP类 文本间加入任意字符的PHP函数 显示code39条形码的PHP类 JavaScript日历
将GBK汉字转化为拼音的Python小程序
网络隐士 · 2005-09-13 · via 博客园 - 网络隐士


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)