欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-20 08:26:10
如下所示: def is_chinese(uchar): """判断一个unicode是否是汉字""" if uchar >= u'\u4e00'...

如下所示:

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="http://i1.go2yd.com/image.php?url=0hd0fkcqse" />迪丽热巴自从签约杨幂工作室,就呈炮竹式的一路走红,特别是参演《三生三世十里桃花》和综艺节目“奔跑吧兄弟”之后,迪丽热巴的人气更是一路看涨,尤其是她在“奔跑吧兄弟”里吃货的人设更是深入人心。<img src="http://i1.go2yd.com/image.php?url=0hd0fk47dk" /><img src="http://i1.go2yd.com/image.php?url=0hd0fkm0u2" />近日,迪丽热巴和张彬彬的新戏《秦时丽人明月心》刚刚热映完,张彬彬的采访被网友挖出,张彬彬说,迪丽热巴从跑男回来之后,体重明星飙升,之前拍戏的时候能轻轻松松的抱着热巴。但是跑男之后,抱上几秒就感觉很吃力了。万万没想到啊,跑男难道不是一个体力类节目吗?热巴硬生生给发展成美食节目了。<img src="http://i1.go2yd.com/image.php?url=0hd0fkhvhw" />更心酸的是,张彬彬实在抱不动了,导演也看不下去了,就说:”让替身上吧!“后来那段戏就是用的迪丽热巴的替身。看来,热巴是该留意一下自己的体重了,虽然外表看不出来胖,但真要发现胖了,对于演员来说,就是致命一击啊。不过,吃货的世界,可能就甘愿承担这样的痛吧。<img src="http://i1.go2yd.com/image.php?url=0hd0fktm1y" />本文为一点号作者原创,未经授权不得转载'
basic_str = format_str(basic_str)
print basic_str

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