0

I have a text file bat.txt containing the following:

  Originator     (#/255)           Nexthop [outgoingIF]:   Potential nexthops ... [B.A.T.M.A.N. adv 0.2-beta r1457, MainIF/MAC: eth0/fe:fe:00:00:01:01]
fe:fe:00:00:05:01  (223) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:03:01 ( 96) fe:fe:00:00:02:01 (223)
fe:fe:00:00:04:01  (233) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:03:01 (100) fe:fe:00:00:02:01 (233)
fe:fe:00:00:02:01  (254) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:02:01 (254) fe:fe:00:00:03:01 (100)
fe:fe:00:00:07:01  (203) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:02:01 (203) fe:fe:00:00:03:01 ( 88)
fe:fe:00:00:08:01  (196) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:03:01 (  0) fe:fe:00:00:02:01 (196)
fe:fe:00:00:03:01  (244) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:03:01 (104) fe:fe:00:00:02:01 (244)
fe:fe:00:00:09:01  (184) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:02:01 (184) fe:fe:00:00:03:01 ( 72)
fe:fe:00:00:06:01  (214) fe:fe:00:00:02:01 [      eth0]: fe:fe:00:00:03:01 ( 76) fe:fe:00:00:02:01 (214)

I want to copy the Originator and the Nexthop columns minus the headings into a python list containing two sublists eg somelist = [[],[]]

with open('bat.txt', 'r') as file:
...     file.readline() # to skip the first line
...     rows = [[int(x) for x in line.split()[:-1]] for line in file]
...     cols = [list(col) for col in zip(*rows)]

This should read all the columns into the list col. What is the best way to select only the columns I want?

1 Answer 1

3

Is this what you're after?

#!/usr/bin/env python

orig=[]
nex=[]

with open("bat.txt") as fd:
    fd.readline()
    for line in fd:
        orig.append(line.split()[0])
        nex.append(line.split()[2])

col=[i for i in zip(orig, nex)]

content of col:

('fe:fe:00:00:05:01', 'fe:fe:00:00:02:01')
('fe:fe:00:00:04:01', 'fe:fe:00:00:02:01')
('fe:fe:00:00:02:01', 'fe:fe:00:00:02:01')
('fe:fe:00:00:07:01', 'fe:fe:00:00:02:01')
('fe:fe:00:00:08:01', 'fe:fe:00:00:02:01')
('fe:fe:00:00:03:01', 'fe:fe:00:00:02:01')
('fe:fe:00:00:09:01', 'fe:fe:00:00:02:01')
('fe:fe:00:00:06:01', 'fe:fe:00:00:02:01')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.