Radhika
Radhika

Reputation: 11

Converting txt file with words into equal-sized matrices in Python

I have a .txt file (pic attached) that for each time step, has a 25 x 19 matrix. I am trying to create a 3D matrix so that I can open the first matrix which will have 25 rows and 19 columns. Essentially, I want to read the entire file besides the lines with Time : [] and cleave the entire file into 50 matrices, each with 25 rows and 19 columns.

I am more familiar with doing this in MATLAB, so any help would be appreciated! Please let me know if you have any questions.

picture of how my text file looks like enter image description here

Upvotes: 0

Views: 191

Answers (1)

ComedicChimera
ComedicChimera

Reputation: 476

In Python, you can open a file and read it line by line by using a for loop like so:

with open('file.txt') as f:
    for line in f:
        ...

Then, you know that the lines you are interested in do not start with the word Time so you can test for that:

with open('file.txt') as f:
    for line in f:
        # skip lines that start with time and empty lines
        if line.startswith('Time') or len(line) == 0:
            continue

Now, we want to create our matrix. Since you seem to have a more data-science-oriented problem, I am going to strongly recommend you use numpy. I am going to create a list of numpy matrices each of which will be 25 x 19. Here's how this would work:

import numpy as np    

# create our list of matrices
matrices = []

# which row we are in as we generate our matrix
line_pos = 0

# loop through each line in the file
with open('file.txt') as f:
    for line in f:
        # know `Time` always begins a new block so we use that to
        # push a new matrix to our list
        if line.startswith('Time'):
            matrices.append(np.zeroes((25, 19)))
            line_pos = 0
            continue
        elif len(line) == 0:
            continue

        # add a new row to the last (top) matrix;
        # basically, split the line by tabs (which appear to be between each element)
        # and then apply a function that first strips away any additional whitespace
        # and then converts the elements to floats
        matrices[-1][line_pos] = list(map(lambda x: float(x.strip()), line.split('\t')))

        # move to insert on next line
        line_pos += 1

Hopefully, this is helpful for you. I don't have a copy of your original data so I can't test if this works, but if it doesn't it should at least give you a place to start from. I am aware the one-liner at the end is a bit complicated, but you said you are a Matlab guy so I assume applying functions to sets of elements is not unfamiliar to you.

Upvotes: 1

Related Questions