栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用Pyton将MAC地址转换成指定格式

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Pyton将MAC地址转换成指定格式

1.环境准备

python3.6
pycharm2021.3.3
相关需要转换的文件

2.前期准备

a.创建文件夹macchange,将需要转换的文件放入。文件夹的目录应在所需要的运行程序的当前目录下!
b.根据当前的格式选择不同的选项。

代码展示部分

主函数部分

import os
import time

import cs
path = os.listdir("macchange")
print(path)
print("1.xx-xx-xx-xx-xx-xx >> xxxx.xxxx.xxxx")
print("2.xxxx-xxxx-xxxx >> xxxx.xxxx.xxxx")
print("3.xx.xx.xx.xx.xx.xx >> xxxx.xxxx.xxxx")
print("please input your chooce:")
key = input()
print(type(key))
while(key not in "123"):
    print("please input key again:")
    key = input()
for i in path:

    newpath = "macchange/"+i
    if key=="1":
        print(i + " start")
        cs.macfun.mac1(newpath)
        print(i + " end")
    if key=="2":
        print(i + " start")
        cs.macfun.mac2(newpath)
        print(i + " end")
    if key=="3":
        print(i + " start")
        cs.macfun.mac3(newpath)
        print(i + " end")
print("please input anykey to exit")
input()

封装功能函数部分

import re
class macfun:
    def mac1(newpath):
        try:
            with open(newpath, "r", encoding='gbk') as f:
                with open(newpath, "r", encoding='utf-8') as f:
                    txt = f.readlines()
                    with open(newpath, "w+") as f1:
                        for line in txt:
                            mac = re.compile(r'(w{2})-(w{2})-(w{2})-(w{2})-(w{2})-(w{2})')
                            line = mac.sub(r"12.34.56", line)
                            f1.write(line)
        except:
            with open(newpath, "r", encoding='utf-8') as f:
                txt = f.readlines()
                with open(newpath, "w+") as f1:
                    for line in txt:
                        mac = re.compile(r'(w{2})-(w{2})-(w{2})-(w{2})-(w{2})-(w{2})')
                        line = mac.sub(r"12.34.56", line)
                        f1.write(line)

    def mac2(newpath):
        try:
            with open(newpath, "r", encoding='gbk') as f:
                txt = f.readlines()
                with open(newpath, "w+") as f1:
                    for line in txt:
                        mac = re.compile(r'(w{4})-(w{4})-(w{4})')
                        line = mac.sub(r"1.2.3", line)
                        f1.write(line)
        except:
            with open(newpath, "r", encoding='utf-8') as f:
                txt = f.readlines()
                with open(newpath, "w+") as f1:
                    for line in txt:
                        mac = re.compile(r'(w{4})-(w{4})-(w{4})')
                        line = mac.sub(r"1.2.3", line)
                        f1.write(line)
    def mac3(newpath):
        try:
            with open(newpath, "r", encoding='gbk') as f:
                txt = f.readlines()
                with open(newpath, "w+") as f1:
                    for line in txt:
                        mac = re.compile(r'(w{2}).(w{2}).(w{2}).(w{2}).(w{2}).(w{2})')
                        line = mac.sub(r"12.34.56", line)
                        f1.write(line)
        except:
            with open(newpath, "r", encoding='utf-8') as f:
                txt = f.readlines()
                with open(newpath, "w+") as f1:
                    for line in txt:
                        mac = re.compile(r'(w{2}).(w{2}).(w{2}).(w{2}).(w{2}).(w{2})')
                        line = mac.sub(r"12.34.56", line)
                        f1.write(line)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号