




免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1. 判断字符串中是否含有汉字。def has_hz(text): hz_yes = False for ch in text: if isinstance(ch, unicode): if unicodedata.east_asian_width(ch)!= Na: hz_yes = True break else: continue return hz_yesdef has_hz(text): hz_yes = False for ch in text: if isinstance(ch, unicode): if unicodedata.east_asian_width(ch)!= Na: hz_yes = True break else: continue return hz_yes 单元测试: assert not has_hz() assert not has_hz( ) assert not has_hz(123) assert not has_hz(u123abc) assert has_hz(u123abc汉字) assert has_hz(u汉字) assert not has_hz() assert not has_hz( ) assert not has_hz(123) assert not has_hz(u123abc) assert has_hz(u123abc汉字) assert has_hz(u汉字)2. 隔指定长度插入一个换行符(/n),一个汉字算2个字符长def get_hz_string_width(text): 获取可能包含汉字的字符串的长度(1个汉字算2个字符长) s = 0 for ch in text: if isinstance(ch, unicode): if unicodedata.east_asian_width(ch)!= Na: s += 2 else: s += 1 else: s += 1 return sdef get_hz_sub_string(text,startat,sub_len=None): 获取可能包含汉字的字符串的子串(计算长度时,1个汉字算2个字符长) 用法: get_hz_sub_string(record,0,44) #取子串,位置为0至43 get_hz_sub_string(record,44) #取子串,位置为44至末尾 s = pos = 0 for ch in text: if pos = startat: s.append(ch) if isinstance(ch, unicode): if unicodedata.east_asian_width(ch)!= Na: pos += 2 else: pos += 1 else: pos += 1 if sub_len!=None and get_hz_string_width(.join(s)=sub_len: break return .join(s)def insert_line_feed(my_str,interval,line_feed=n): 隔指定长度插入一个n符号(一个汉字处理为2个字符长度) if len(my_str)=0: return n = int(get_hz_string_width(my_str)-1)/interval)+1 str_list = k = 1 pos_start = 0 while k = startat: s.append(ch) if isinstance(ch, unicode): if unicodedata.east_asian_width(ch)!= Na: pos += 2 else: pos += 1 else: pos += 1 if sub_len!=None and get_hz_string_width(.join(s)=sub_len: break return .join(s)def insert_line_feed(my_str,interval,line_feed=/n): 隔指定长度插入一个/n符号(一个汉字处理为2个字符长度) if len(my_str)=0: return n = int(get_hz_string_width(my_str)-1)/interval)+1 str_list = k = 1 pos_start = 0 while k 0: line_str = str_listi if len(line_str.strip()=0: text_to_line = i i -= 1 else: break new_str_list = i = 0 for obj in str_list: if do_trim and i = text_to_line: break new_str_list += split(insert_line_feed(obj,line_length),n) i += 1 #不加 u 就出错“unicode object is not callable”!? return u+n.join(new_str_list) def wrap_text_block(text,line_length,do_trim=True): if do_trim: str_list = split(text.rstrip(),/n) else: str_list = split(text,/n) #检测末尾空行的开始位置 text_to_line = -1 if do_trim: i = len(str_list)-1 while i 0: line_str = str_listi if len(line_str.strip()=0: text_to_line = i i -= 1 else: break new_str_list = i = 0 for obj in str_list: if do_trim and i = text_to_line: break new_str_list += split(insert_line_feed(obj,line_length),/n) i += 1 #不加 u 就出错“unicode object is not callable”!? return u+/n.join(new_str_list) 单元测试: assert wrap_text_block(,1)= assert wrap_text_block(,1,do_trim=False)= assert wrap_text_block(u文字1234,2)=u文n字n12n34 assert wrap_text_block(u文字12345 ,2)=u文n字n12n34n5 assert wrap_text_block(u文字1n234,2)=u文n字n1n23n4 assert wrap_text_block(u文字1n2345 ,2)=u文n字n1n23n45 assert wrap_text_block(,1)= assert wrap_text_block(,1,do_trim=False)= assert wrap_text_block(u文字1234,2)=u文/n字/n12/n3
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 合伙包鱼塘协议书
- 购买项目协议书
- 购买蛋糕协议书
- 钱财就会协议书
- 订房服务协议书
- 伊百丽代理协议书
- 酒泉农业协议书
- 订制门窗协议书
- 赠品使用协议书
- 钻孔取芯协议书
- 风机基础降水施工实施方案
- 门禁系统施工技术方案
- 《婴幼儿健康管理》课件-任务四 婴幼儿健康档案建设与管理
- 【出口退税管理探究的国内外探究综述4300字】
- 2024版小学语文新课程标准
- 水文地质技术员技能鉴定理论考试题库-上(单选题)
- 2024年保密教育线上培训考试
- 服饰搭配艺术(山东联盟)智慧树知到期末考试答案章节答案2024年德州学院
- 船舶避碰 第十七条直航船的行动
- 绿色矿山造林施工合同
- 中国绝经管理与绝经激素治疗指南(2023版)解读
评论
0/150
提交评论