[Python] Insert ":" in MAC address

I am making my own packet capture to study the network. I was writing a process to parse Ethernet frames in that. When the raw data received by the socket is converted into a character string, the MAC address is It is acquired without the semicolon ":" inserted.

I want to insert a semicolon


000c29ecc97a
↓ I want to do this ↓
00:0c:29:ec:c9:7a

Since it is a string list, should I divide it into sublists of 2 characters each and join with ":"? When I googled it, I found zip (\ * [iter (s)] * n) as a technique to divide it into n-character sublists.

Insert a semicolon by splitting and joining


>>> s = '000c29ecc97a'
>>> zip(*[iter(s)]*2)
>>> [('0', '0'), ('0', 'c'), ('2', '9'), ('e', 'c'), ('c', '9'), ('7', 'a')]
# ":"Connect with!
>>> ':'.join(map(''.join, zip(*[iter(s)]*2)))
'00:0c:29:ec:c9:7a'

':'.join(map(''.join, zip(*[iter(s)]*2)))But***It's hard to read***。


 It's a good technique, but it's unpleasant because it's hard to understand without knowing it.
 The next thing I came up with was how to replace it with a regular expression.


#### **`Insert a semicolon with a regular expression`**
```pycon

>>> import re
>>> s = '000c29ecc97a'
>>> raw_mac_ptn = r'([0-9A-F]{2})(?!$)'
>>> mac_addr = re.sub(raw_mac_ptn, r'\1:', s)
>>> mac_addr
'00:0c:29:ec:c9:7a'

I wonder if this one is easier to see. Negative look-ahead (?! $) Is important so that ":" is not inserted at the end.

Recommended Posts

[Python] Insert ":" in MAC address
Hash method (open address method) in Python
[Introduction] Insert line breaks in Python 3
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
python on mac
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
Get your own IP address in Python
flatten in python
[Mac] Run the RealSense D415 sample in Python
Try working with Mongo in Python on Mac
Think about building a Python 3 environment in a Mac environment
Insert an object inside a string in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python