在我们收到客户需要更改的mac地址是通常是XX-XX-XX-XX-XX-XX的形式、或者XX:XX:XX:XX:XX:XX形式的mac地址,不能直接刷入需要我们将MAC地址格式化为XXXX-XXXX-XXXX的形式。非常麻烦,可以使用python转换。
以下是python源码
import re def format_mac(mac): cleaned_mac = re.sub(r'[^0-9a-fA-F]', '', mac) # 将MAC地址格式化为XXXX-XXXX-XXXX的形式 formatted_mac = '-'.join([cleaned_mac[i:i + 4] for i in range(0, len(cleaned_mac), 4)]) return formatted_mac # 读取文件 file_path = "input.txt" try: with open(file_path, 'r') as file: # 逐行读取文件内容 for line in file: # 替换MAC地址并打印 modified_mac = format_mac(line.strip()) print(modified_mac) except FileNotFoundError: print(f"文件 '{file_path}' 不存在。") except Exception as e: print(f"发生错误:{e}")
以下是python演示
我们需要创建一个名为input.txt的文件放到同目录下,然后把需要转换的MAC复制进去,最后运行就可以自动转换。