vendredi 20 février 2015

Python - how to match specific words / digits from multiple lines in a text file and store them in separate lists

I have a .txt file, and it's like this one:



ip sla logging traps
ip sla 2553
ethernet jitter mpid 6553 domain Gravity vlan 2553 num-frames 100 interval 100
tag CSCO5839
frequency 300
ip sla schedule 2553 life forever start-time now
ip sla 3553
ethernet jitter mpid 7553 domain Gravity vlan 3553 num-frames 100 interval 100
tag CSCO5839
frequency 300
ip sla schedule 3553 life forever start-time now


And I would like a method to match the ip sla / mpid / vlan and tag and store their values into a list or a vector:


Desired output:


ipsla[0]=2553 and ipsla[1]=3553

mpid[0]=6553 and mpid[1]=7553

vlan[0]=2553 and vlan[1]=3553

tag[0]=CSCO5839


Now, I have just started learning Python and I know that I have to parse each line of the file, then match the desired result using re.match(), and then store those obtained results in an array, or a list.


My poor code so far:



#!/usr/bin/env python

import re

myFile = open("regex.txt","r")

for line in myFile:
if re.match("",line): #I should have a condition there but I got lost
#now I have to store that variable in an array / or a list


My questions so far:

- what expression should I use so that I can find what I need ?

- How can I , then, store those values, each two in a vector / list with the same name ? eg: ipsla[0], ipsla[1] ?


I would also like some explanations if it is possible.


Thank you.


Aucun commentaire:

Enregistrer un commentaire