I have a kind of complex problem here in parsing a text file.
What I need:
Read through a text file.
If a line matches a specific condition, create a key named (condition 1)
Copy the lines that follow as a list. this list needs to be associated with key (Condition 1)
When the condition is encountered again, a new key and copy the lines following and repeat step 3 until the end of file
Problem: I am having trouble appending new items in the list for a given key
Sample Text Input file:
A1 letters characters jgjgjg
A2 letters numbers fgdhdhd
D1 letters numbers haksjshs
condition1, dhdjfjf
K2 letters characters jgjgjg
J1 alphas numbers fgdhdhd
L1 letters numbers haksjshs
condition2, dhdjfjf
J1 alphas numbers fgdhdhd
D1 letters numbers haksjshs
J1 alphas numbers fgdhdhd
D1 letters numbers haksjshs
Expected Dictionary:
dictone = {'condition1':['K2 letters characters jgjgjg','J1 alphas numbers fgdhdhd','L1 letters numbers haksjshs'], 'condition2':['J1 alphas numbers fgdhdhd','D1 letters numbers haksjshs','J1 alphas numbers fgdhdhd','D1 letters numbers haksjshs'..........}
Here is what I have done thus far..
flagInitial = False # flag to start copy after encountering condition
with open(inputFilePath, "r") as tfile:
for item in tfile:
gcmatch = gcpattern.match(item)
if gcmatch:
extr = re.split(' ', item)
laynum = extr[2]
newKey = item[2:7] + laynum[:-1]
flagInitial = True
gcdict[newKey] = item
continue
if flagInitial == True:
gcdict[newKey].append(item) # stuck here
# print(gcdict[newKey])
# print(newKey)
Am I missing syntax or something ?
Try this:
With
re.search
function andcollection.defaultdict
object:The output: