Read data from a txt file into 2D array python

user3600460 picture user3600460 · Jul 14, 2018 · Viewed 12.7k times · Source

I'm fairly new to python and I would like to know if I could get some assistance with the problem I'm trying to solve:

I would like to design a loop to iterate over every file in the directory and place the data into a 2D array for every file. I have a large directory of .txt files that contain 22 lines of 2 numbers per line.

An example of how the files contents will be organized is:

# Start of file_1.txt
1 2
3 4
5 6
7 8

# Start of file 2.txt
6 7
8 9
3 4
5 5

I would like to read the data separated by whitespace into the first two reference locations in the array(i.e. array = [x0][y0]) , and at the newline, write the following data into the next location of the array (i.e. array=[x1][y2]). I see a lot of people saying to use numpy, scipy, and other methods but that is confusing me further.

The output that I am seeking is:

[[1,2],[3,4],[5,6],[7,8], ...]

I'm bit stuck on how to iterate through the files in a directory and simultaneously place them into a 2D array. The code I have so far is:

import os
trainDir = 'training/'
testDir = 'testing/'
array2D = []

for filename in os.listdir(trainDir,testDir):
    if filename.endswith('.txt'):
        array2D.append(str(filename))

print(array2D)

At the moment the above code does not work for two directories, but it does for one. Any help would be appreciated.

Answer

Andrej Kesely picture Andrej Kesely · Jul 14, 2018

You are defining your array2D wrong at the beginning, that's not valid Python syntax. The following code should work:

import os

d = 'HERE YOU WRITE YOUR DIRECTORY'

array2D = []

for filename in os.listdir(d):
    if not filename.endswith('.pts'):
        continue

    with open(filename, 'r') as f:
        for line in f.readlines():
            array2D.append(line.split(' '))

print(array2D)