Reputation: 1505
I have the following code:
inputFile = open('C:/Abaqus_JOBS' + JobDir + '/' + JobName + '-3_4.inp', 'r')
for line in inputFile:
fileData.append([x.strip() for x in line.split(',')])
fel=0
for row,data in enumerate(fileData):
if data[0]=='*Node':
row_nodes = row #number of the row when data='*Node'
if data[0]=='*Element' and fel==0:
row_elements2 = row
fel=1
for row,data in enumerate(fileData[row_nodes + 1:row_elements2]):
nodes.append(data) #data between '*Nodes' and '*Element'
However, it runs very slow (minutes) in the python interpeter of an external program (I have to run the script here because I need to access a database of results produced by this program). How can I optimize it?
EDIT:
I close the inputFile
at the end of the code: inputFile.close()
Upvotes: 0
Views: 138
Reputation: 1280
If I understand well, you first store the file line by line, then search for the first occurence of "*Element" and the last occurence of "*Node", and finally store what's between them.
An optimization I see is that you can go from 3 parsing of your file to a single one:
inputFile = open('C:/Abaqus_JOBS' + JobDir + '/' + JobName + '-3_4.inp', 'r')
go_storage = False
nodes = None
for line in inputFile:
if line[0] == "*Node":
# Reset what has already been memorized
nodes = list()
go_storage = True
elif line[0] == "*Element":
break
elif go_storage:
nodes.append(line)
Upvotes: 3
Reputation: 2573
Maybe you could think along the lines of regular expressions:
if I understand it right, you want to get the data between the keywords *Node and *Element in some file, right?
well you could try something like:
import re
S = open('C:/Abaqus_JOBS' + JobDir + '/' + JobName + '-3_4.inp','r').read()
Data = re.finditer( "\*Nonde([.\n]*?)\*Element", S )
That should give you a list of strings that are found in between the Tags "*Node" and "*Elements"
I hope that was what you were trying to do. Cheers
Upvotes: 1