版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全工作隐患排查治理实施方案3篇
- 慢病预防的健康信息标准化建设
- 包装回收合作服务协议书
- 2026年国际快递电子签收合同
- 2026年三国知识竞赛试题及答案
- 遥感监测无人机动力系统操作规程
- 演艺节目编排方案
- 2026年急救技能考核模拟练习题
- 慢病防控政策执行中的基层医疗能力提升策略
- 慢病跨境管理中的资源整合策略
- 2025云南昆明巫家坝城市发展建设有限公司社会招聘14人参考笔试题库及答案解析
- 新《增值税法实施条例》逐条解读课件
- 汽车维修技师招聘面试问题及答案
- 守护花蕾计划课件
- 2025年尾矿库年度排放计划
- 莉芙敏片临床应用
- 农田农作物种植布局规划方案
- 2025年10月自考03291人际关系学试题及答案
- T-JYBZ 044-2025 研学旅行基地建设评估指南
- 高楼燃气管道施工方案
- 清华大学-算力电力协同:思路与探索白皮书(2025 年)
评论
0/150
提交评论