How to obtain the first letter of a group of Chinese characters and Pinyin using python

Source: Internet
Author: User

How to obtain the first letter of a group of Chinese characters and Pinyin using python

This example describes how to obtain the first letter of a group of Chinese Characters in python. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

#! /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 = U' Welcome'

Main (str_input)

I hope this article will help you with Python programming.

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.