1.判断字符串中是否含有汉字。defhas_hz(text):hz_yes=Falseforchintext:ifisinstance(ch,unicode):ifunicodedata.east_asian_width(ch)!='Na':hz_yes=Truebreakelse:continuereturnhz_yesdefhas_hz(text):hz_yes=Falseforchintext:ifisinstance(ch,unicode):ifunicodedata.east_asian_width(ch)!='Na':hz_yes=Truebreakelse:continuereturnhz_yes单元测试:assertnothas_hz()assertnothas_hz()assertnothas_hz(123)assertnothas_hz(u123abc)asserthas_hz(u123abc汉字)asserthas_hz(u汉字)assertnothas_hz()assertnothas_hz()assertnothas_hz(123)assertnothas_hz(u123abc)asserthas_hz(u123abc汉字)asserthas_hz(u汉字)2.隔指定长度插入一个换行符(/n),一个汉字算2个字符长defget_hz_string_width(text):获取可能包含汉字的字符串的长度(1个汉字算2个字符长)s=0forchintext:ifisinstance(ch,unicode):ifunicodedata.east_asian_width(ch)!='Na':s+=2else:s+=1else:s+=1returnsdefget_hz_sub_string(text,startat,sub_len=None):获取可能包含汉字的字符串的子串(计算长度时,1个汉字算2个字符长)用法:get_hz_sub_string(record,0,44)#取子串,位置为0至43get_hz_sub_string(record,44)#取子串,位置为44至末尾s=[]pos=0forchintext:ifpos=startat:s.append(ch)ifisinstance(ch,unicode):ifunicodedata.east_asian_width(ch)!='Na':pos+=2else:pos+=1else:pos+=1ifsub_len!=Noneandget_hz_string_width(''.join(s))=sub_len:breakreturn''.join(s)definsert_line_feed(my_str,interval,line_feed=\n):隔指定长度插入一个\n符号(一个汉字处理为2个字符长度)iflen(my_str)==0:returnn=int((get_hz_string_width(my_str)-1)/interval)+1str_list=[]k=1pos_start=0whilek=n:sub_str=get_hz_sub_string(my_str,pos_start,interval)str_list.append(sub_str)k=k+1pos_start=pos_start+get_hz_string_width(sub_str)returnline_feed.join(str_list)defget_hz_string_width(text):获取可能包含汉字的字符串的长度(1个汉字算2个字符长)s=0forchintext:ifisinstance(ch,unicode):ifunicodedata.east_asian_width(ch)!='Na':s+=2else:s+=1else:s+=1returnsdefget_hz_sub_string(text,startat,sub_len=None):获取可能包含汉字的字符串的子串(计算长度时,1个汉字算2个字符长)用法:get_hz_sub_string(record,0,44)#取子串,位置为0至43get_hz_sub_string(record,44)#取子串,位置为44至末尾s=[]pos=0forchintext:ifpos=startat:s.append(ch)ifisinstance(ch,unicode):ifunicodedata.east_asian_width(ch)!='Na':pos+=2else:pos+=1else:pos+=1ifsub_len!=Noneandget_hz_string_width(''.join(s))=sub_len:breakreturn''.join(s)definsert_line_feed(my_str,interval,line_feed=/n):隔指定长度插入一个/n符号(一个汉字处理为2个字符长度)iflen(my_str)==0:returnn=int((get_hz_string_width(my_str)-1)/interval)+1str_list=[]k=1pos_start=0whilek=n:sub_str=get_hz_sub_string(my_str,pos_start,interval)str_list.append(sub_str)k=k+1pos_start=pos_start+get_hz_string_width(sub_str)returnline_feed.join(str_list)单元测试:assertinsert_line_feed(,1)==assertinsert_line_feed(1,1)==1assertinsert_line_feed(1234567890,5)==12345\n67890assertinsert_line_feed(u汉字1汉字234567890,5)==u汉字1\n汉字2\n34567\n890assertinsert_line_feed(u汉字1汉字234567890,4)==u汉字\n1汉字\n2345\n6789\n0assertinsert_line_feed(,1)==assertinsert_line_feed(1,1)==1assertinsert_line_feed(1234567890,5)==12345/n67890assertinsert_line_feed(u汉字1汉字234567890,5)==u汉字1/n汉字2/n34567/n890assertinsert_line_feed(u汉字1汉字234567890,4)==u汉字/n1汉字/n2345/n6789/n03.按指定长度为文字块分行(类似Word效果),并取消末尾的空行。defwrap_text_block(text,line_length,do_trim=True):ifdo_trim:str_list=split(text.rstrip(),'\n')else:str_list=split(text,'\n')#检测末尾空行的开始位置text_to_line=-1ifdo_trim:i=len(str_list)-1whilei0:line_str=str_list[i]iflen(line_str.strip())==0:text_to_line=ii-=1else:breaknew_str_list=[]i=0forobjinstr_list:ifdo_trimandi==text_to_line:breaknew_str_list+=split(insert_line_feed(obj,line_length),'\n')i+=1#不加u''就出错“'unicode'objectisnotcallable”!?returnu''+'\n'.join(new_str_list)defwrap_text_block(text,line_length,do_trim=True):ifdo_trim:str_list=split(text.rstrip(),'/n')else:str_list=split(text,'/n')#检测末尾空行的开始位置text_to_line=-1ifdo_trim:i=len(str_list)-1whilei0:line_str=str_list[i]iflen(line_str.strip())==0:text_to_line=ii-=1else:breaknew_str_list=[]i=0forobjinstr_list:ifdo_trimandi==text_to_line:breaknew_str_list+=split(insert_line_feed(obj,line_length),'/n')i+=1#不加u''就出错“'unicode'objectisnotcallable”!?returnu''+'/n'.join(new_str_list)单元测试:assertwrap_text_block(,1)==assertwrap_text_block(,1,do_trim=False)==assertwrap_text_block(u文字1234,2)==u文\n字\n12\n34assertwrap_text_block(u文字12345,2)==u文\n字\n12\n34\n5assertwrap_text_block(u文字1\n234,2)==u文\n字\n1\n23\n4assertwrap_text_block(u文字1\n2345,2)==u文\n字\n1\n23\n45assertwrap_text_block(,1)==assertwrap_text_block(,1,do_trim=False)==assertwrap_text_block(u文字1234,2)==u文/n字/n12/n34assertwrap_text_block(u文字12345,2)==u文/n字/n12/n34/n5assertwrap_text_block(u文字1/n234,2)==u文/n字/n1/n23/n4assertwrap_text_block(u文字1/n2345,2)==u文/n字/n1/n23/n45