当前第2页 返回上一页
方法二:
代码示例:
1 2 3 4 5 6 7 8 9 10 | import re
A = open ( 'mac.txt' , 'r' )
a = A.readlines()
for aa in a:
b = re.findall(r '.{2}' ,aa)
c = '-' .join(b)
print c
A.close()
|
使用用python的正则表达式实现,执行效率高,值得推荐。
处理结果:
50-E5-49-E3-2E-CB
90-2B-34-13-EF-A6
50-E5-49-EC-BA-1C
90-2B-34-57-B1-6F
1C-6F-65-29-6D-F9
90-2B-34-13-1A-14
50-E5-49-E3-E2-F8
50-E5-49-3A-26-96
90-2B-34-5F-B0-21
90-2B-34-13-15-74
90-2B-34-18-43-BF
00-24-1D-0E-25-8D
python处理字符串还是很牛的,建议大家牢固掌握。
python按照固定长度分割字符串三个字符一组
1 2 3 4 5 6 7 8 | def cut_text(text,lenth):
textArr = re.findall( '.{' + str (lenth) + '}' , text)
textArr.append(text[( len (textArr) * lenth):])
return textArr
print (cut_text( '123456789abcdefg' , 3 ))
[ '123' , '456' , '789' , 'abc' , 'def' , 'g' ]
|
代码二
1 2 3 4 5 | >>> import re
>>> string = '123456789abcdefg'
>>> re.findall(r '.{3}' , string)
[ '123' , '456' , '789' , 'abc' , 'def' ]
>>>
|
相关推荐:
python print 按逗号或空格分隔的方法
Python 实现字符串中指定位置插入一个字符
以上就是python 按照固定长度分割字符串的方法的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
深入认识Python中的itertools模块
Python中整数的最大可能值是多少?(代码示例)
Python中判断语句与循环语句的简单小结(附示例)
Python 怎么把列表的[]去掉
Python阶乘求和的方法
怎么查看一个对象的类型
Python如何判断一个文件是否存在
Python源程序执行的方式有哪几种
Python如何生成随机密码?
Python randint怎么用
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python 按照固定长度分割字符串的方法