版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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
2、: 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(u12
3、3abc汉字) 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): 获取
4、可能包含汉字的字符串的子串(计算长度时,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_le
5、n!=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 isinstan
6、ce(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_s
7、tring_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)
8、 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 = st
9、r_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) 单元测试:
10、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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年羽绒行业分析报告及未来发展趋势报告
- 2026年激光电视行业分析报告及未来发展趋势报告
- 关节炎患者的疼痛管理非药物疗法
- 2026年DSP广告行业分析报告及未来发展趋势报告
- 中医肾结石的日常保健方法
- 偏瘫患者康复护理短期计划
- 内科护理中的出院指导
- 医疗保健与紧急救治制度
- 2021电化学储能电站并网运行与控制技术规范 第9部分:仿真计算模型与参数实测
- 小初中高中小学网络安全教育主题班会说课稿2025
- 江苏省昆山市、太仓市2026届中考历史模试卷含解析
- 2026年宝鸡市辛家山马头滩林业局招聘(12人)笔试参考试题及答案详解
- 2026届甘肃省武威市天祝藏族自治县第一中学高三下学期学科素养评价练习(二)历史试题(含答案)
- 养老护理员服务意识与责任感培养
- 2026年天津市和平区中考一模英语试卷和答案
- 地理信息安全在线培训考试系统题库及答案
- 2026中国生物可吸收胶原蛋白植入物行业发展形势与前景动态预测报告
- 2026年江苏省南通市中小学教师招聘考试真题及答案
- 护士工作制度及流程
- 2026年质量管理体系注册审核员考试题库(附答案)
- 2025-2026学年地理台湾省教学设计
评论
0/150
提交评论