采用保留文本,汉字,字母方法剔除异常字符
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if uchar >= u'\u4e00' and uchar <= u'\u9fa5':
return True
else:
return False
def is_number(uchar):
"""判断一个unicode是否是数字"""
if uchar >= u'\u0030' and uchar <= u'\u0039':
return True
else:
return False
def is_alphabet(uchar):
"""判断一个unicode是否是英文字母"""
if (uchar >= u'\u0041' and uchar <= u'\u005a') or (uchar >= u'\u0061' and uchar <= u'\u007a'):
return True
else:
return False
def format_str(content):
content_str = ''
for i in content:
if is_chinese(i):
content_str = content_str+i
elif is_alphabet(i):
content_str = content_str + i
elif is_number(i):
content_str = content_str + i
return content_str
print(format_str("我好像/、 &*(不爱吃草莓了),ok?"))