# -*- coding: gbk -*-
def getGBK(s):
i = 0
first = (int('81',16),int('FE',16))
second1 = (int('40',16),int('7E',16))
second2= (int('80',16),int('FE',16))
buf = []
while True:
ch1 = ord(s[i])
if ch1 >= first[0] and ch1 <= first[1]:
i = i + 1
if i >= len(s):
break
ch2 = ord(s[i])
if (ch2 >= second1[0] and ch2 <= second1[1]) or \
(ch2 >= second2[0] and ch2 <= second2[1]):
buf.append(chr(ch1))
buf.append(chr(ch2))
i = i + 1
else:
i = i + 1
if i >= len(s):
break
print ''.join(buf)
getGBK('我 是 谁')
getGBK('@我是 谁%$%')
getGBK('@我abc是谁%$%')
运行结果
C:\>test.py
我是谁
我是谁
我是谁