python处理字符串


#将mac地址更改成一定格式,如mac='902B345FB021'改为mac='90-2B-34-5F-B0-21',
#其实就是字符串按照固定长度拆分。2位数字拆分
 
#!/usr/bin/python
# -*- coding: utf-8 -*-
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()
 
每两个字符拆出来,放入一个列表,再用join连接
 

评论关闭