I need to write a script recently. I need to get the first letter of Chinese characters and pinyin. I checked some materials online and found it is easy to implement. I would like to share it with you. This script is used for the first letter of Chinese Characters in pinyin, for example, "I am a Chinese". The resulting letter is wszgr.
- #! /Usr/bin/env python
- #-*-Coding: UTF-8 -*-
- Def multi_get_letter (str_input ):
- If isinstance (str_input, unicode ):
- Unicode_str = str_input
- Else:
- Try:
- Unicode_str = str_input.decode ('utf8 ')
- Except t:
- Try:
- Unicode_str = str_input.decode ('gbk ')
- Except t:
- Print 'unknown coding'
- Return
- Return_list = []
- For one_unicode in unicode_str:
- Return_list.append (single_get_first (one_unicode ))
- Return return_list
-
- Def single_get_first (unicode1 ):
- Str1 = unicode1.encode ('gbk ')
- Try:
- Ord (str1)
- Return str1
- Except t:
- 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 main (str_input ):
- A = multi_get_letter (str_input)
- B =''
- For I in:
- B = B + I
- Print B
- If _ name _ = "_ main __":
- Str_input = 'I am a China'
- Main (str_input)
Run the following command:
650) this. width = 650; "border =" 0 "alt =" "src =" http://img1.51cto.com/attachment/201209/085337988.jpg "/>
This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/983289