Python REGEX and File I/O -


still learning python , new regex. trying information text file , put list later processing:

here sample python file:

import re  text = '''name = file details version = v1.2 ;---------------- ; notes on line 1 ; notes on line 2 ; ; notes on line four, skipping line 3 ;-------------- configuring device configuring device ; don't want note'''    def notes(path):     file = re.split('\n+', path)     outputname = outputver = outputnote = ''     notes = []     outputnotes = []     line in file:         name = re.search('^name = (.*)$', line)         ver = re.search('^version = (.*)$', line)         note = re.search('; (.*)', line)         if name:             outputname = name.group(1)         if ver:             outputver  = ver.group(1)         notes.append(note)     note in notes:         print(note)        info = (outputname, outputver, outputnotes)     print(info[2])      notes in info[2]:         if notes:             print(notes)      print(info)   notes(text) 

what want grab "name", "version" , "notes"

i can name , version no issues, notes i'm having problem with. notes want in between ;--------- marks. not want notes later in file.

essentially, want output like:

('file details', 'v1.2', ['notes on line one', 'notes on line two', '','notes on line four, skipping line 3']) 

also, i'm sure there ways optimize this, i'd interested hear suggestions.

if understand problem statement, reading varying number of lines @ top of file. there no reason use regex @ - read 2 lines name , version, read header start line (';---') loop, reading lines array, until see header end line (';---').


Comments