Nima Geran
Nima Geran

Reputation: 125

Extracting lists of data from a text file

My problem is how to extract two lists of numbers from a text file which is given from a finite element program (ABAQUS). In fact, the X column in the text is the time and the Force-1 is the force. After getting the two lists, I want to do some mathematical operations to reach my goal.

The text file I have is as follows: Besides the first line, there exists other empty lines but I can't tell the number of them exactly at the moment. For example the last four lines of my text file are empty and really I don't know which of the other lines are also empty.

<blank line>
              X               FORCE-1
<blank line>
            0.                 0.
           10.E-03            98.3479E+03
           12.5E-03          122.947E+03
           15.E-03           147.416E+03
           18.75E-03         183.805E+03
           22.5E-03          215.356E+03
           26.25E-03         217.503E+03
           30.E-03           218.764E+03
           33.75E-03         219.724E+03
           37.5E-03          220.503E+03
           43.125E-03        221.938E+03
           51.5625E-03       228.526E+03
           61.5625E-03       233.812E+03
<blank line>
<blank line>
<blank line>
<blank line>

Could you tell me how to write this code in python that could find this empty lines and only extract the numbers and then get the two lists of numbers as follows, for example for X(time):

['0.', '10.E-03', '12.5E-03', '15.E-03', '18.75E-03', '22.5E-03', '26.25E-03', '30.E-03', '33.75E-03', '37.5E-03', '43.125E-03', '51.5625E-03', '61.5625E-03']

(I asked a similar question just a minute ago and I got some answers that didn't help me after all!)

Upvotes: 1

Views: 101

Answers (1)

Julien
Julien

Reputation: 5729

You have to loop over each line of the file, create conditions to skip over irrelevant lines, and manipulate the remaining lines to extract the data into your data structures. Explanation in the code comments:

with open("my_file.txt", "rb") as f: # Open file in read-binary mode
    lines = f.readlines() # Load all lines

x = []
force = []
for line in lines:
    try:
        stripped = line.strip() # Remove extra surrounding whitespace
        if stripped: # This will be false if the line is empty
            x_value, force_value = stripped.split() # Split by the middle whitespace into two values
            # Append data to each list
            # Pass a scientific notation string to float() will make the data more usable
            x.append(float(x_value))
            force.append(float(force_value))
    except ValueError
        pass

print x
print force

EDIT: The original answer was cutting off the first line, but since you were still hitting errors, I instead just skip lines that don't contain the data values by using a try: except: block. What happens is the header value X get's passed into float(), and since it's not parsable into a float, it raises a ValueError. Since it's in the try statement block, it looks to see if we're handling ValueError in the except block. We are, so it runs this code without raising the error. Since we just pass, we continue iteration to the next line.

Upvotes: 1

Related Questions