简体   繁体   中英

Extract only desired columns from a txt file using Python?

I have written a script that outputs Cisco switch data to a .txt file. I want to parse these files to extract only the information I need from them using Python. Below is an example of data in the text file:

VLAN         Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1        default                          active    Gi0/3, Gi1/0, Gi1/1, Gi1/2, Gi1/3, Gi2/1, Gi2/2, Gi2/3

95        MGMT-VLAN95                      active    Gi0/2

1002        fddi-default                     act/unsup

1003        token-ring-default               act/unsup

1004         fddinet-default                  act/unsup

1005        trnet-default                    act/unsup p

1005        trnet-default                    act/unsup

With my limited knowledge of Python I have tried to achieve this by doing the following but I'm not managing to achieve what I wish:

with open('newfile', 'rw') as newfile:
    list = newfile.readlines()
    for row in list:
        x = (row.strip().split())
        print(x)
  • Print(x) returns the below output but when I try to use indexing and call print(x[1]) I receive a Traceback error message to this line.
['VLAN', 'Name', 'Status', 'Ports']
['----', '--------------------------------', '---------', '-------------------------------']
['1', 'default', 'active', 'Gi0/3,', 'Gi1/0,', 'Gi1/1,', 'Gi1/2,', 'Gi1/3,', 'Gi2/0,', 'Gi2/1,', 'Gi2/2,', 'Gi2/3,', 'Gi3/0,', 'Gi3/1,', 'Gi3/2,', 'Gi3/3']
['95', 'MGMT-VLAN95', 'active', 'Gi0/2']
['1002', 'fddi-default', 'act/unsup']
['1003', 'token-ring-default', 'act/unsup']
['1004', 'fddinet-default', 'act/unsup']
['1005', 'trnet-default', 'act/unsup', 'p']
['1005', 'trnet-default', 'act/unsup']

Below is the output I am hoping for:

VLAN Name 
---- -------------------------------- 
1    default
95   VLAN-MGMT95
200  VLAN200
1002 fddi-default
1003 token-ring-default
1004 fddinet-default
1005 trnet-default

If you just want the first two columns just do this:

with open('newfile', 'rw') as nf:
    mylist = nf.readlines()
    for row in mylist:
        mydata = row[0] + " " + row[1]
        print mydata

(note I changed your variable because "list" is a Python keyword).

Not sure what your traceback is, but I would recommend using the following:

with open('newfile', 'rw') as newfile:
    lines = newfile.readlines()
    for row in lines:
        x = row.strip().split()
        if len(x) >= 2:
            print(x[0].ljust(4) + ' ' + x[1])

This will avoid possible issues with malformed lines causing IndexError exceptions from popping-up. Also, ljust(4) gives the output nicer formatting by left-justifying the string in the VLAN column with four spaces.

You can do it like this:

with open('data.txt') as f:
    lines = [line.strip() for line in f.readlines()]
    lines_to_lists = [line.split() for line in lines if line]
    content = list(zip(*lines_to_lists))

This way each column from your file goes to the particular place (index) in a content list. So if you want to print your VLANS just type:

print(content[0])

If you want to print your VLANS names:

print(content[1])

For both:

for vlan, name in zip(content[0], content[1]):
    print(vlan, name)

You can use TTP to parse above text, here is the code:

from ttp import ttp

data = """
VLAN         Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1        default                          active    Gi0/3, Gi1/0, Gi1/1, Gi1/2, Gi1/3, Gi2/1, Gi2/2, Gi2/3

95        MGMT-VLAN95                      active    Gi0/2

1002        fddi-default                     act/unsup

1003        token-ring-default               act/unsup

1004         fddinet-default                  act/unsup

1005        trnet-default                    act/unsup

1005        trnet-default                    act/unsup
"""

template = """
<group method="table">
{{ VLAN | DIGIT }}  {{ name }}  {{ ignore }}  {{ ignore(ORPHRASE) }}
{{ VLAN | DIGIT }}  {{ name }}  {{ ignore }}
</group>

<output format="tabulate" headers="VLAN,name"/>
"""

parser = ttp(data, template)
parser.parse()
print(parser.result()[0])

will rpint:

  VLAN  name
------  ------------------
     1  default
    95  MGMT-VLAN95
  1002  fddi-default
  1003  token-ring-default
  1004  fddinet-default
  1005  trnet-default
  1005  trnet-default
with open('newfile', 'r') as newfile:
    list = newfile.readlines()
    for row in list:
        x = (row.strip().split())
        y = x[0:2]
        print(', '.join(y))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM