-1

I have this code in MATLAB

txtFiles = dir('*.txt') ; %loads txt files
N = length(txtFiles) ; 

for i = 1:N
    data = importdata(txtFiles(i).name);
    x = data(:,1);
    y(:,i) = data(:,2) ;
end

Which takes all 100 of my txt files and creates an array for x then stores the y data in an a separate array where each column corresponds to a different txt file's values.

Is there a similar trick in Python?

this is how the data files are constructed:

896.5000000000 0.8694710776
896.7500000000 0.7608314184
897.0000000000 0.6349069122
897.2500000000 0.5092121001
897.5000000000 0.3955858698
jimbob97
  • 61
  • 5

1 Answers1

1

You could put this in many ways:

Using Single Liners

import glob
# This can be further shortened, but will result in a less clear code.
# One quality of a pythonic code

# Gets file names and reads them.
data = [open(file).read() for file in glob.glob('*.txt')]

# each block of data has it's line split and is put
# into seperate arrays

# [['data1 - line1', 'data1 - line2'], ['data2 - line1',...]]
data = [block.splitlines() for block in data]

x = [] # for column-1
y = [] # for column-2

# takes each line within each file data
# splits it, and appends to x and y

data = [
        # this splits by the spaces
        # example line: -2000        data1
        # output  = ['-2000', 'data']

        x.append(line.split()[0]) or y.append(line.split()[1])
        # splits it, and appends to x and y
        for file_data in data for line in file_data
        # takes each line within each file data
        ]

Without Single Liners

import glob

x, y = [], []
# x : column 1
# y : column 2

# For each text file in dir
for file in glob.glob('*.txt'):

    # open the file
    with open(file) as _:

        # read and splitlines
        for line in _.read().splitlines():

            # split the columns
            line = line.split()

            # this splits by the spaces
            # example line: -2000        data1
            # output  = ['-2000', 'data']

            # append to lists
            x.append(line[0])
            y.append(line[1])

result:

print (x) # ['896.5000000000', '896.7500000000', '897.0000000000', '897.2500000000', '897.5000000000']
print (y) # ['0.8694710776', '0.7608314184', '0.6349069122', '0.5092121001', '0.3955858698']
  • ahh ok so the x array for me goes from -2000 to 2000 and I pulled it out separately, is that what you did here? – jimbob97 Oct 23 '22 at 10:22
  • 1
    @jimbob97 I didn't quite catch that, what do you mean from -2000? :) – linux.manifest Oct 23 '22 at 11:02
  • Yeah so the txt files have 2 columns, the first one ranges from -2000 to 2000 and the other contains the simulation data. I want to pull the first column as x and then pull the data into another array where each column is a different txt file – jimbob97 Oct 23 '22 at 11:55
  • oh I see, I'll edit my answer.. but one I'll need a two or three lines to see, how the data is structured in the text file line1: `column 1` - `column 2` line2: `range1` - `data1` line3: `range2` - `data2` – linux.manifest Oct 23 '22 at 12:15
  • Sure, this is how it's structured (edited question) – jimbob97 Oct 23 '22 at 12:25
  • ok I edited my answer with the output – linux.manifest Oct 23 '22 at 12:33
  • Ok so what should I do now if I have 50 text files like this one and I want to add each of their second columns to y so I can plot each realisation separately, in matlab I'd do y(:, 1) – jimbob97 Oct 23 '22 at 14:49
  • To plot, that would be a separate question, you could post that you the following data and how or which libraries could you use for plotting. – linux.manifest Oct 23 '22 at 15:47
  • I can't see how to separate each of them into columns thogh – jimbob97 Oct 23 '22 at 16:13
  • but the code above already does that and adds the second column to the `y` array (see the output above) and it should do it for all 50 files if the code file is in the same dir. – linux.manifest Oct 23 '22 at 16:59