Reputation: 27
I have this txt and I need to parse it in a Python script.
00:08:e3:ff:fd:90 - 172.21.152.1
70:70:8b:85:67:80 - 172.21.155.4
I want to separate and store in an array only the MAC address. How can I do this?
Upvotes: 1
Views: 66
Reputation: 181
You can use the built-in function open to read the file, giving the path to the file and passing the "r" argument to tell that you want to read the file. Then use the readlines function from the file handler which returns a list of lines. For each line you can split the text on the dash character. The mac address will be the first element in the list given by the split function.
with open("file.txt", "r") as f :
macs = [line.split(" - ")[0] for line in f.readlines()]
Upvotes: 3
Reputation: 7164
You could achieve this with pandas as well
import pandas as pd
macs = pd.read_table('file.txt', header=None, usecols=[0], delim_whitespace=True)
I think it would be unnecessary to use pandas for this purpose only. However, if you are using pandas already anyway, I would prefer this approach
Upvotes: 1