python 关于格式转换的处理方法dict.itervalues() + dict.format()
2017-08-12 11:57
323 查看
问题:
如何处理相同内容,不同显示格式的统一处理?
解决方案:
采用dict.itervalues()方法,获取每一个不同的格式类型
采用re.search(格式,内容)方法,找到对应的格式,并将匹配到的内容存储为一个元组
采用dict.format(*元组)方法,将该元组格式化为统一的格式,并输出
讨论:
格式必须事先全部确定,缺少灵活性
# -*- coding:utf-8 -*-
import os
import sys
import re
print u"mac地址转换"
print u'采用pattens匹配的方法实现'
mac1 = '00.11.22.33.44.55'
mac2 = '00:11:22:33:44:55'
mac3 = '0011.2233.4455'
mac4 = '00-11-22-33-44-55'
mac5 = '001122334455'
search_pattens = {
0: r"(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})",
1: r"(\w{2})(\w{2})\.(\w{2})(\w{2})\.(\w{2})(\w{2})",
2: r"(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2})",
3: r"(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2})",
4: r"(\w{2})\.(\w{2})\.(\w{2})\.(\w{2})\.(\w{2})\.(\w{2})"
}
format_pattens = {
0: "{0}{1}{2}{3}{4}{5}",
1: "{0}{1}.{2}{3}.{4}{5}",
2: "{0}:{1}:{2}:{3}:{4}:{5}",
3: "{0}-{1}-{2}-{3}-{4}-{5}",
4: "{0}.{1}.{2}.{3}.{4}.{5}"
}
def mac_convert(mac, mac_type=1):
"""
Args:
mac: 输入的MAC,格式自动判断
mac_type: 输出MAC的格式,默认转换成type1。
| 0 : 010203040506 |
| 1 : 0102.0304.0506 |
| 2 : 01:02:03:04:05:06 |
| 3 : 01-02-03-04-05-06 |
| 4 : 01.02.03.04.05.06 |
Returns: 输出的MAC大小写不做改变
"""
data = []
for sp in search_pattens.itervalues():
m = re.search(sp, mac)
if m:
data = m.groups() # 返回元组
print m.group() # 返回数值
break
if len(data) is not 6:
print u"[MAC CONVERT ERROR] 输入MAC %s 格式不正确" % mac
return ""
mac_type = int(mac_type)
fp = format_pattens[mac_type]
return fp.format(*data)
if __name__ == '__main__':
print mac1
print mac_convert(mac1)
如何处理相同内容,不同显示格式的统一处理?
解决方案:
采用dict.itervalues()方法,获取每一个不同的格式类型
采用re.search(格式,内容)方法,找到对应的格式,并将匹配到的内容存储为一个元组
采用dict.format(*元组)方法,将该元组格式化为统一的格式,并输出
讨论:
格式必须事先全部确定,缺少灵活性
# -*- coding:utf-8 -*-
import os
import sys
import re
print u"mac地址转换"
print u'采用pattens匹配的方法实现'
mac1 = '00.11.22.33.44.55'
mac2 = '00:11:22:33:44:55'
mac3 = '0011.2233.4455'
mac4 = '00-11-22-33-44-55'
mac5 = '001122334455'
search_pattens = {
0: r"(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})",
1: r"(\w{2})(\w{2})\.(\w{2})(\w{2})\.(\w{2})(\w{2})",
2: r"(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2})",
3: r"(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2})-(\w{2})",
4: r"(\w{2})\.(\w{2})\.(\w{2})\.(\w{2})\.(\w{2})\.(\w{2})"
}
format_pattens = {
0: "{0}{1}{2}{3}{4}{5}",
1: "{0}{1}.{2}{3}.{4}{5}",
2: "{0}:{1}:{2}:{3}:{4}:{5}",
3: "{0}-{1}-{2}-{3}-{4}-{5}",
4: "{0}.{1}.{2}.{3}.{4}.{5}"
}
def mac_convert(mac, mac_type=1):
"""
Args:
mac: 输入的MAC,格式自动判断
mac_type: 输出MAC的格式,默认转换成type1。
| 0 : 010203040506 |
| 1 : 0102.0304.0506 |
| 2 : 01:02:03:04:05:06 |
| 3 : 01-02-03-04-05-06 |
| 4 : 01.02.03.04.05.06 |
Returns: 输出的MAC大小写不做改变
"""
data = []
for sp in search_pattens.itervalues():
m = re.search(sp, mac)
if m:
data = m.groups() # 返回元组
print m.group() # 返回数值
break
if len(data) is not 6:
print u"[MAC CONVERT ERROR] 输入MAC %s 格式不正确" % mac
return ""
mac_type = int(mac_type)
fp = format_pattens[mac_type]
return fp.format(*data)
if __name__ == '__main__':
print mac1
print mac_convert(mac1)
相关文章推荐
- values of type NSInteger should not be used as format arguments; 关于Xcode中烦人的32位与64位警告处理方法.
- Python中关于时间处理的方法
- .NET 关于从客户端(...)中检测到有潜在危险的Request.Form 值的处理办法 找不到方法:System.Web.UnvalidatedRequestValues报错的处理方案(待完善)
- dict里面的itervalues() 方法比 values()
- 关于python的bottle框架跨域请求报错问题的处理方法
- Python的dict类型迭代性能的优化(items()与iteritems()、keys()与iterkeys()、values()与itervalues()的区别)
- Opencv3.0-python的那些事儿:(九)、Opencv关于如何读取大量视频帧进行处理的方法
- python关于dict的几个方法探讨
- Python+selenium 关于富文本形式的评论框处理方法
- [原创]关于www.17777.com的恶意修改的处理方法
- 处理Clob数据(转)关于oracle中大对象处理的一些方法和实例
- 关于SQL SERVER 日志满的处理方法
- 关于SQL SERVER日志满或过大的处理方法
- 关于Windows任务栏不显示的处理方法
- 关于Windows任务栏不显示的处理方法
- 关于Eclipse无法重新编译java文件的处理方法之一
- java关于日期的运算等处理方法
- 关于Eclipse无法重新编译java文件的处理方法之一
- 关于嵌套事务的一种处理方法
- PYTHON 2.2 IDLE 中文字串处理设定方法