venkat
venkat

Reputation: 31

My Python regex code is not giving me the output as i expected

I was trying to extract and parse the words (Hostname & version) from a text file. when i run my code it writes the data into csv file but the output looks different. output i had after parsing to csv

 **my input file is .txt and below is the content** 
 Hostname Router1
 version 15.01
 code:
 line console 0
 logging synchronous
 exec-timeout 15 1
 usb-inactivity-timeout 15
 exec prompt timestamp
 transport preferred none

Hostname Router2
version 15.02
line vty 0 15
logging synchronous
exec-timeout 15 2
exec prompt timestamp
transport input ssh
transport preferred none
access-class REMOTE_ACCESS in

Hostname Router3
version 15
line console 0
logging synchronous
exec-timeout 15 3
usb-inactivity-timeout 15
exec prompt timestamp
transport preferred none

Hostname Router3
version 15.12
line vty 0 15
logging synchronous
exec-timeout 15 4
exec prompt timestamp
transport input ssh
transport preferred none
access-class REMOTE_ACCESS in
**Above is the sample content in my input text file**
$
import re
import csv
with open('sample5.csv','w',newline='') as output:
    HeaderFields = ['Hostname','version']
    writer = csv.DictWriter(output,fieldnames=HeaderFields)
    writer.writeheader()
with open('testfile.txt','r',encoding='utf-8') as input:
    for line in input.readlines():
       pattern = re.compile(r'Hostname(.*)''|''version(.*)')
       match=pattern.finditer(line)
       for match1 in match:
          with open('sample5.csv', 'a',newline='') as output:
              writer = csv.DictWriter(output, fieldnames=HeaderFields)
              writer.writerow({'Hostname': match1.group(1), 'version':  
                                match1.group(2)})

My expected result in csv is as follows: This should be the result in csv

Thank You.

Upvotes: 1

Views: 77

Answers (1)

Serge
Serge

Reputation: 3765

You code fails because in each iteration you read only one line (which can contain host or version but not both, yet you write data into csv. Let's iterate through all the text while matching twoliners: with first line Hostname.. and second line version... \n works as line break for Windows (I hear Mac uses \r not sure). Now since you match twoliners you can grab both router and version from same match object.

with open('testfile.txt','r',encoding='utf-8') as input:
       txt = input.read()
       pattern = re.compile(r'Hostname (.*)(\r\n?|\n)version (.*)')
       match=pattern.finditer(txt)
       for match1 in match:
          with open('sample5.csv', 'a',newline='') as output:
             writer = csv.DictWriter(output, fieldnames=HeaderFields)
             writer.writerow({'Hostname': match1.group(1), 'version':  
                            match1.group(3)})

Upvotes: 2

Related Questions