0

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()

erbridge
1,38612 silver badges27 bronze badges
asked May 16, 2013 at 3:18
8
  • What's in the ....? Commented May 16, 2013 at 3:22
  • 1
    how big is the file? Are you sure you're closing the file properly after reading it? Commented May 16, 2013 at 3:22
  • @Patashu, jozzas: question edited. Commented May 16, 2013 at 3:27
  • Are you forced to write it in Python, or are you able to write it in a faster language? Commented May 16, 2013 at 3:30
  • @Patashu: I can write in C++.... but I'd have to learn it. Commented May 16, 2013 at 3:33

2 Answers 2

3

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) 
answered May 16, 2013 at 9:14
Sign up to request clarification or add additional context in comments.

Comments

1

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

answered May 16, 2013 at 7:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.