python小点心--获取汉子拼音首字母
2016-03-07 19:55
537 查看
def single_get_first(unicode1): str1 = unicode1.encode('gbk') try: return chr(ord(str1)) except: asc = ord(str1[0])*256 + ord(str1[1]) - 65536 if asc >= -20319 and asc <= -20284: return 'a' if asc >= -20283 and asc <= -19776: return 'b' if asc >= -19775 and asc <= -19219: return 'c' if asc >= -19218 and asc <= -18711: return 'd' if asc >= -18710 and asc <= -18527: return 'e' if asc >= -18526 and asc <= -18240: return 'f' if asc >= -18239 and asc <= -17923: return 'g' if asc >= -17922 and asc <= -17418: return 'h' if asc >= -17417 and asc <= -16475: return 'j' if asc >= -16474 and asc <= -16213: return 'k' if asc >= -16212 and asc <= -15641: return 'l' if asc >= -15640 and asc <= -15166: return 'm' if asc >= -15165 and asc <= -14923: return 'n' if asc >= -14922 and asc <= -14915: return 'o' if asc >= -14914 and asc <= -14631: return 'p' if asc >= -14630 and asc <= -14150: return 'q' if asc >= -14149 and asc <= -14091: return 'r' if asc >= -14090 and asc <= -13119: return 's' if asc >= -13118 and asc <= -12839: return 't' if asc >= -12838 and asc <= -12557: return 'w' if asc >= -12556 and asc <= -11848: return 'x' if asc >= -11847 and asc <= -11056: return 'y' if asc >= -11055 and asc <= -10247: return 'z' return '' def getPinyin(string): if string==None: return None lst = list(string) charLst = [] for l in lst: charLst.append(single_get_first(l)) return ''.join(charLst) if __name__=='__main__': print getPinyin(u'非常好')一定要传入unicode才可以
相关文章推荐
- python小点心-logging 日志模块
- Python使用mechanize模拟浏览器
- Python爬虫经验
- python模块之 - logging
- python小点心--内存文件映射
- python 登录并爬取淘宝信息
- 获取腾讯企业邮箱通讯录python脚本(带登录功能)
- python小点心--struct 封装变长字符串
- mac下更新pip时提示OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pip-8.0.2-py2.
- python格式化输出dict等集合对象
- [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
- Python django学习
- [LeetCode]题解(python):104-Maximum Depth of Binary Tree
- [LeetCode]题解(python):103-Binary Tree Zigzag Level Order Traversal
- C\C#\Java\Python 基本数据类型比较
- [LeetCode]题解(python):102- Binary Tree Level Order Traversal
- windows系统下Python环境的搭建
- [LeetCode]题解(python):101-Symmetric Tree
- 【python】编程语言入门经典100例--24
- Numpy使用1