The first letter of the Chinese pinyin alphabet in python.

Source: Internet
Author: User

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.

 

 
 
  1. #! /Usr/bin/env python
  2. #-*-Coding: UTF-8 -*-
  3. Def multi_get_letter (str_input ):
  4. If isinstance (str_input, unicode ):
  5. Unicode_str = str_input
  6. Else:
  7. Try:
  8. Unicode_str = str_input.decode ('utf8 ')
  9. Except t:
  10. Try:
  11. Unicode_str = str_input.decode ('gbk ')
  12. Except t:
  13. Print 'unknown coding'
  14. Return
  15. Return_list = []
  16. For one_unicode in unicode_str:
  17. Return_list.append (single_get_first (one_unicode ))
  18. Return return_list
  19.  
  20. Def single_get_first (unicode1 ):
  21. Str1 = unicode1.encode ('gbk ')
  22. Try:
  23. Ord (str1)
  24. Return str1
  25. Except t:
  26. Asc = ord (str1 [0]) * 256 + ord (str1 [1])-65536
  27. If asc> =-20319 and asc <=-20284:
  28. Return 'A'
  29. If asc> =-20283 and asc <=-19776:
  30. Return 'B'
  31. If asc> =-19775 and asc <=-19219:
  32. Return 'C'
  33. If asc> =-19218 and asc <=-18711:
  34. Return 'D'
  35. If asc> =-18710 and asc <=-18527:
  36. Return 'E'
  37. If asc> =-18526 and asc <=-18240:
  38. Return 'F'
  39. If asc> =-18239 and asc <=-17923:
  40. Return 'G'
  41. If asc> =-17922 and asc <=-17418:
  42. Return 'H'
  43. If asc> =-17417 and asc <=-16475:
  44. Return 'J'
  45. If asc> =-16474 and asc <=-16213:
  46. Return 'K'
  47. If asc> =-16212 and asc <=-15641:
  48. Return 'l'
  49. If asc> =-15640 and asc <=-15166:
  50. Return 'M'
  51. If asc> =-15165 and asc <=-14923:
  52. Return 'n'
  53. If asc> =-14922 and asc <=-14915:
  54. Return 'O'
  55. If asc> =-14914 and asc <=-14631:
  56. Return 'P'
  57. If asc> =-14630 and asc <=-14150:
  58. Return 'Q'
  59. If asc> =-14149 and asc <=-14091:
  60. Return 'R'
  61. If asc> =-14090 and asc <=-13119:
  62. Return's'
  63. If asc> =-13118 and asc <=-12839:
  64. Return 'T'
  65. If asc> =-12838 and asc <=-12557:
  66. Return 'W'
  67. If asc> =-12556 and asc <=-11848:
  68. Return 'X'
  69. If asc> =-11847 and asc <=-11056:
  70. Return 'y'
  71. If asc> =-11055 and asc <=-10247:
  72. Return 'Z'
  73. Return''
  74.  
  75. Def main (str_input ):
  76. A = multi_get_letter (str_input)
  77. B =''
  78. For I in:
  79. B = B + I
  80. Print B
  81. If _ name _ = "_ main __":
  82. Str_input = 'I am a China'
  83. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.