Read text files in a folder into a martix

Viewed 74

I have some text files in a folder and I need to read all of them into a matrix, but when I use the following code:

with open('E:/UGC/Animationpsnr_bitrate_0.txt','r') as file:
    psnr_bitrate = file.readlines()

It produces an str list but I need to save them as float variables. In the following I put a sample of text file content:

33.773083 36.171250 38.032833 39.589417 40.937167 42.214250
45.867417 49.271917 51.478500 53.247500 54.981250 56.507333
48.421417 51.848833 54.390500 56.687750 58.457333 60.029583
48.790583 52.691333 55.534750 57.793250 59.953167 61.538500

Suppose I have 20 text files in a folder with contents like above. finally, I need to read all files and save them in a matrix that has 6 columns and the rows are all the text file values. could you please tell me how can I do this?

3 Answers

We can glob to iterate over all the pathnames of text files in the folder UGC, then using np.loadtxt you can load the txt files as a numpy array, finally use np.vstack to vertically stack all the arrays

import glob

np.vstack([np.loadtxt(path, dtype='float') for path in glob.iglob(r'E:/UGC/*.txt')])

The following code will find any files within the directory that matches bitrate*.txt. It will then iterate over each line, convert each value to a float and add it to the list data

from pathlib import Path

path = Path(r'C:\YourFolder')
print(path)

data = []

for filepath in path.glob('bitrate*.txt'):
    with open(filepath,'r') as file:
        for line in file:
            data.append([float(x) for x in line.split()])
            
print(data)

Pandas/numpy not required here.

you can split each line and convert it to float like this:

matrix = []
with open('E:/UGC/Animationpsnr_bitrate_0.txt', 'r') as file:
    lines = file.readlines()
    for line in lines:
        matrix.append([float(value) for value in line.strip().split()])

for multiple files add one more for loop at top and read each file and append the contents on matrix list.

Related