Today, a colleague gave me a bunch of mac addresses that need to be changed to a certain format. For example, if mac = '902b345fb021 'is changed to mac = '90-2B-34-5F-B0-21', he wrote a small python script, help. In fact, the string is split according to a fixed length.
There are a bunch of mac addresses in the mac.txt file: There are two methods to implement the scheme:
- Method 1:
- Aw.open('mac.txt ', 'R ')
- A = A. readlines ()
- Foraaina:
- B = list (aa. strip ())
- C =''
- Foriinrange (len (B )):
- Ifi! = 0:
- Ifi % 2 = 0:
- C = c + '-' + B [I]
- Else:
- C = c + B [I]
- Else:
- C = c + B [I]
- Printc
- A. close ()
- This method is relatively simple. It was just thought of at first.
- Method 2:
- Importre
- Aw.open('mac.txt ', 'R ')
- A = A. readlines ()
- Foraaina:
- B = re. findall (R'. {2} ', aa)
- C = '-'. join (B)
- Printc
- A. close ()
- This method is implemented using python regular expressions, which is convenient and highly efficient in execution.
The processing result is as follows:
50-E5-49-E3-2E-CB
90-2B-34-13-EF-A6
50-E5-49-EC-BA-1C
90-2B-34-57-B1-6F
1C-6F-65-29-6D-F9
90-2B-34-13-1A-14
50-E5-49-E3-E2-F8
50-E5-49-3A-26-96
90-2B-34-5F-B0-21
90-2B-34-13-15-74
90-2B-34-18-43-BF
00-24-1D-0E-25-8D