import re
import csv
with open("data.txt") as infile:
text = infile.read()
regex = re.compile(
r"""^(?P<device>\S*) # Match non-whitespace device name
\suptime\sis\s # Match " uptime is "
(?P<uptime>[^\r\n]*) # Match until end of line --> uptime
.*?^System\simage\sfile\sis\s # Match intervening text
"[^:]*: # Match from quote to colon
(?P<sifilename>[^"]*) # Match everything until quote --> filename
.*?^cisco\s # Match intervening text
(?P<model>\S*) # Match non-whitespace model name
.*?^Processor\sboard\sID\s # Match intervening text
(?P<serialno>[^\r\n]*) # Match until end of line --> serial no""",
re.DOTALL | re.MULTILINE | re.VERBOSE)
match = regex.search(text)
with open("results.txt", "a") as outfile:
outcsv = csv.writer(outfile)
outcsv.writerow(match.groups())