I am trying to modify a txt file with ~43k lines. After the command *Nset is given in the file, I need to extract and save all of the lines following that command, stopping when it gets to the next *command in the file. There is a different number of lines and characters after each of the commands. For instance, here's a sample part of the file:
*Nset
1, 2, 3, 4, 5, 6, 7,
12, 13, 14, 15, 16,
17, 52, 75, 86, 92,
90, 91, 92 93, 94, 95....
*NEXT COMMAND
blah blah blah
*Nset
numbers
*Nset
numbers
*Command
irrelevant text
The code I currently have works when the numbers I need are not in between two *Nset's. When one *Nset follows another's numbers, it skips that command and the proceeding lines all together and I can't figure out why. When the next command is not *Nset, it finds the next one and pulls out the data perfectly fine.
import re
# read in the input deck
deck_name = 'master.txt'
deck = open(deck_name,'r')
#initialize variables
nset_data = []
matched_nset_lines = []
nset_count = 0
for line in deck:
# loop to extract all nset names and node numbers
important_line = re.search(r'\*Nset,.*',line)
if important_line :
line_value = important_line.group() #name for nset
matched_nset_lines.insert(nset_count,line_value) #name for nset
temp = []
# read lines from the found match up until the next *command
for line_x in deck :
if not re.match(r'\*',line_x):
temp.append(line_x)
else :
break
nset_data.append(temp)
nset_count = nset_count + 1
I'm using Python 3.5. Thanks for any help.