= u'\u4e00' and uchar <= u'\u9fa5':return Trueelse:return Falsedef is_number(uchar):"""判断一个 python 字符串只保留汉字的方法 黑松山资源网更新日期:2025/3/15" />

站长资源脚本专栏

python 字符串只保留汉字的方法

整理:jimmy2025/3/15浏览2
简介如下所示:def is_chinese(uchar):"""判断一个unicode是否是汉字"""if uchar >= u'\u4e00' and uchar <= u'\u9fa5':return Trueelse:return Falsedef is_number(uchar):"""判断一个

如下所示:

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 = unicode(content,'utf-8')
 content_str = ''
 for i in content:
  if is_chinese(i):
   content_str = content_str+i
 return content_str
basic_str = '<img src="/UploadFiles/2021-04-08/image.php">

以上这篇python 字符串只保留汉字的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。