判断字符串中是否含有汉字

来源:互联网 发布:国家卫计委网络直报 编辑:程序博客网 时间:2024/04/30 09:30

 关键字: 汉字字符串

1. 判断字符串中是否含有汉字。

 

 

Python代码
  1. def has_hz(text):  
  2.     hz_yes = False  
  3.     for ch in text:  
  4.         if isinstance(ch, unicode):  
  5.             if unicodedata.east_asian_width(ch)!= 'Na':  
  6.                 hz_yes = True  
  7.                 break  
  8.         else:  
  9.             continue     
  10.          
  11.     return hz_yes  

 单元测试:

Python代码
  1. assert not has_hz("")  
  2. assert not has_hz("  ")  
  3. assert not has_hz("123")  
  4. assert not has_hz(u"123abc")  
  5. assert has_hz(u"123abc汉字")  
  6. assert has_hz(u"汉字")  

 

2.隔指定长度插入一个换行符(/n),一个汉字算2个字符长。

 

Python代码
  1. def get_hz_string_width(text):  
  2.     """ 
  3.     获取可能包含汉字的字符串的长度(1个汉字算2个字符长) 
  4.     """  
  5.     s = 0  
  6.     for ch in text:  
  7.         if isinstance(ch, unicode):  
  8.             if unicodedata.east_asian_width(ch)!= 'Na':   
  9.                 s += 2  
  10.             else:  
  11.                 s += 1  
  12.         else:  
  13.             s += 1  
  14.     return s  
  15.   
  16. def get_hz_sub_string(text,startat,sub_len=None):  
  17.     """ 
  18.     获取可能包含汉字的字符串的子串(计算长度时,1个汉字算2个字符长) 
  19.      
  20.     用法: 
  21.     get_hz_sub_string(record,0,44)  #取子串,位置为0至43 
  22.     get_hz_sub_string(record,44)    #取子串,位置为44至末尾 
  23.     """  
  24.     s = []  
  25.     pos = 0  
  26.     for ch in text:  
  27.         if pos >= startat:  
  28.             s.append(ch)  
  29.         if isinstance(ch, unicode):  
  30.             if unicodedata.east_asian_width(ch)!= 'Na':   
  31.                 pos += 2  
  32.             else:  
  33.                 pos += 1  
  34.         else:  
  35.             pos += 1  
  36.         if sub_len!=None and get_hz_string_width(''.join(s))>=sub_len:  
  37.             break     
  38.     return ''.join(s)  
  39.   
  40. def insert_line_feed(my_str,interval,line_feed="/n"):  
  41.     """隔指定长度插入一个/n符号(一个汉字处理为2个字符长度)"""  
  42.     if len(my_str)==0:   
  43.         return ""  
  44.       
  45.     n = int((get_hz_string_width(my_str)-1)/interval)+1  
  46.     str_list = []  
  47.     k = 1  
  48.     pos_start = 0  
  49.     while k <= n:  
  50.         sub_str = get_hz_sub_string(my_str,pos_start,interval)   
  51.         str_list.append(sub_str)  
  52.         k = k + 1  
  53.         pos_start = pos_start + get_hz_string_width(sub_str)  
  54.           
  55.     return line_feed.join(str_list)      

 单元测试:

Python代码
  1. assert insert_line_feed("",1)==""  
  2. assert insert_line_feed("1",1)=="1"  
  3. assert insert_line_feed("1234567890",5)=="12345/n67890"  
  4. assert insert_line_feed(u"汉字1汉字234567890",5)==u"汉字1/n汉字2/n34567/n890"  
  5. assert insert_line_feed(u"汉字1汉字234567890",4)==u"汉字/n1汉字/n2345/n6789/n0"  

 

3. 按指定长度为文字块分行(类似Word效果),并取消末尾的空行。

 

Python代码
  1. def wrap_text_block(text,line_length,do_trim=True):  
  2.     if do_trim:  
  3.         str_list = split(text.rstrip(),'/n')  
  4.     else:      
  5.         str_list = split(text,'/n')  
  6.       
  7.     #检测末尾空行的开始位置  
  8.     text_to_line = -1  
  9.     if do_trim:  
  10.         i = len(str_list)-1  
  11.         while i > 0:  
  12.             line_str = str_list[i]  
  13.             if len(line_str.strip())==0:  
  14.                 text_to_line = i  
  15.                 i -= 1  
  16.             else:  
  17.                 break       
  18.       
  19.     new_str_list = []  
  20.     i = 0  
  21.     for obj in str_list:  
  22.         if do_trim and i == text_to_line:  
  23.             break  
  24.         new_str_list += split(insert_line_feed(obj,line_length),'/n')  
  25.         i += 1  
  26.   
  27.     #不加 u'' 就出错“'unicode' object is not callable”!?  
  28.     return u''+'/n'.join(new_str_list)      
 

单元测试:

Python代码
  1. assert wrap_text_block("",1)==""  
  2. assert wrap_text_block("",1,do_trim=False)==""  
  3.   
  4. assert wrap_text_block(u"文字1234",2)==u"文/n字/n12/n34"  
  5. assert wrap_text_block(u"文字12345    ",2)==u"文/n字/n12/n34/n5"  
  6.   
  7. assert wrap_text_block(u"文字1/n234",2)==u"文/n字/n1/n23/n4"  
  8. assert wrap_text_block(u"文字1/n2345    ",2)==u"文/n字/n1/n23/n45"  
 
原创粉丝点击