Python Reading a file and printing -


hi reading file data.txt in below format.

    last table change time   : 6:55:12 ago     number of table inserts  : 3     number of table deletes  : 0     number of table drops    : 0     number of table age-outs : 0      port       neighbor device id             neighbor port id           ttl     et1        arista2                        ethernet1                  120     et2        arista2                        ethernet2                  120     ma1        arista2                        management1                120 

i need extract data , print

et1, arista2, ethernet1 et2, arista2, ethernet2 ma1, arista2, management1 

i using below code, able print

('et1', 'et2', 'ma1')

    open('data.txt') f:         x in xrange(6):             next(f)         line in f:             print zip(*[line.split() line in f])[0]         f.close() 

you can extract desired content of little bit regex.

try following code snippet:

import re  open('input.txt','r') fp:     x in xrange(7):         next(fp)      rx = "\w+"     line in fp:         data = re.findall(u"\w+", line, re.dotall)         if data:             print(', '.join(data[0:-1])) 

depending on file content , format, print

et1, arista2, ethernet1 et2, arista2, ethernet2 ma1, arista2, management1 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -