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
| #!/usr/bin/env python
# -*- encoding: 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:
try:
unicode_str = str_input.decode('gbk')
except:
print 'unknown coding'
return
return_list = []
for one_unicode in unicode_str:
#print single_get_first(one_unicode)
return_list.append(single_get_first2(one_unicode))
return "".join(return_list)
def single_get_first2(unicode1):
str1 = unicode1.encode('gbk')
try:
ord(str1)
return str1
except:
asc = ord(str1[0]) * 256 + ord(str1[1]) - 65536
asc_list = (-20320, -20284, -19776, -19219,
-18711, -18527, -18240, -17923,
-17418, -16475, -16213, -15641,
-15166, -14923, -14915, -14631,
-14150, -14091, -13119, -12839,
-12557, -11848, -11056, -10247)
letter_list = ('a', 'b', 'c', 'd',
'e', 'f', 'g', 'h',
'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q',
'r', 's', 't', 'w',
'x', 'y', 'z')
for i in range(0, len(letter_list)):
if asc >= (asc_list[i]+1) and asc <= asc_list[i+1]:
return letter_list[i]
return ''
def printresult(str):
print('中文: "%s" --> 首字母拼音: "%s"' % (str, multi_get_letter(str)))
if __name__ == '__main__':
printresult('木哈哈')
printresult('小李')
printresult('大王')
printresult('大d王m')
printresult('哦i哎v')
printresult('啊吧才的恶发跟好就看了卖你哦怕去染色体我小样猪')
|