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_yes
单元测试:
assert not has_hz("")
assert not has_hz(" ")
assert not has_hz("123")
assert not has_hz(u"123abc")
assert has_hz(u"123abc汉字")
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 s
def 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 <= n:
sub_str = get_hz_sub_string(my_str,pos_start,interval)
str_list.append(sub_str)
k = k + 1
pos_start = pos_start + get_hz_string_width(sub_str)
return line_feed.join(str_list)
单元测试:
assert insert_line_feed("",1)==""
assert insert_line_feed("1",1)=="1"
assert insert_line_feed("1234567890",5)=="12345\n67890"
assert insert_line_feed(u"汉字1汉字234567890",5)==u"汉字1\n汉字2\n34567\n890"
assert insert_line_feed(u"汉字1汉字234567890",4)==u"汉字\n1汉字\n2345\n6789\n0"
3. 按指定长度为文字块分行(类似Word效果),并取消末尾的空行。
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_list[i]
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字\n12\n34"
assert wrap_text_block(u"文字12345 ",2)==u"文\n字\n12\n34\n5"
assert wrap_text_block(u"文字1\n234",2)==u"文\n字\n1\n23\n4"
assert wrap_text_block(u"文字1\n2345 ",2)==u"文\n字\n1\n23\n45"
分享到:
相关推荐
使用中文命名的大疆机甲大师Python例程 使用中文命名的大疆机甲大师Python例程 使用中文命名的大疆机甲大师Python例程 使用中文命名的大疆机甲大师Python例程 使用中文命名的大疆机甲大师Python例程 使用中文命名的...
图形界面编辑,各种python语言基础语句运用,以及各种库的教程
Python例程1.2-入门(conditionals,exceptions,for loop,function, generator, simple loop, while loop, error info)-Python examples 1.2-Quick Start(conditionals,exceptions,for loop,function, ...
Python简明教程例程
适合新手练习,初学者最合适,里面含有大量的python基础pdf文档
用Python3编写的,采用递归调用技术实现查找指定磁盘路径范围内包含给定字符串的文本文件功能的例程
(如本人本机python36.dll在D:\program files (x86)\Microsoft Visual Studio\Shared\Python36_86目录下)。3,测试时将zip压缩包全部解压到同一目录下。4,若有时间可封装支持库。资源作者:。@cs666。资源下载:。
qt调用python脚本例程.rar
入门级别的python代码,刚学python的朋友可以试着看一看.
python numpy库的使用 python基础,演示了numpy库的使用,提供python2例程代码,适合初学者 Numpy Numpy是Python的一个科学计算的库 高效的向量和矩阵计算 很多函数都是C语言实现的 import numpy as np 用法:np.###...
pygame的实用例程,实现了几个用python开发的小游戏
VC++图像转换为PDF,文件操作,邮件发送,网络发送等实用例程
这是一段来自美国加州大学尔湾分校教授的一段Python教学例程
通过调用PythonW解释器的方式,进行Python结果回调。已封装成类,方便调用。源码和例子全部发出来了,需要的自己研究下吧。用源码之前先安装下环境。@applek。
python 条件同步的使用 条件同步:threading.Condition 多线程同步,python2例程 多线程的同步 多线程情况下最常见的问题之一:数据共享; 当多个线程都要去修改某一个共享数据的时候,需要对数据访问进行同步...
语音信号处理——Python中用于信号处理的一小部分例程_Python_下载.zip
python 队列的使用 python2例程展示了队列的使用过程,供学习参考使用 队列:Queue queue_test.py put()函数主动改变队列 get()函数阻塞,代替查询 Produser & Consumer 定期生产,一有货就被抢购 ...
Python udp socket 连接的简单例程
STM32参考程序繁星开发板STM32-MPU6050_python上位机例程