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"\1\2.\3\4.\5\6", 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"\1\2.\3\4.\5\6", 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"\1\2.\3\4.\5\6", 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"\1\2.\3\4.\5\6", line)
f1.write(line)