Loading values from a textfile as float and removing empty space

Question:

I am currently trying to load all values from every text file inside a directory. My text file has around 110k values and the last value is always an empty space for some reason. So it ends up giving an error saying it can’t convert the text values to float.

An example of the structure of the values inside a text file:

25.243431
25.340025
25.243431
25.267595
25.219267
25.267595
25.267595
25.340025
25.36419 
blank space here <

And the following is what I tried:

import os, glob


path = "C:/Users/myUser/Desktop/folder/"
os.chdir(path)

temperature_values = []
def txt_to_lst(file_path):
    temp = []
    try:
        stopword=open(file_path,"r")
        lines = stopword.read().split('n')
        temp.append(lines)
        temp = np.array(temp)
    except Exception as e:
        print(e)
        
    return temp
for file in os.listdir():
    if file.endswith(".txt"):
        file_path = f"{path}{file}"
        temperature_values.append(txt_to_lst(file_path))
        temperature_values = [i for i in temperature_values if i]

It gives me the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

without the following line temperature_values = [i for i in temperature_values if i]

My code loads successfully all values but as string ['24.977287',..., '24.928835', ''], including the empty string. What I would like to do is filter the blank space as a I load the files and convert them to float.

Asked By: xnok

||

Answers:

You can use list comprehension:

with open('foo.txt') as f:
    nums = [float(line) for line in map(str.strip, f) if line != '']

print(nums)
# [25.243431, 25.340025, 25.243431, 25.267595, 25.219267, 25.267595, 25.267595, 25.340025, 25.36419]
Answered By: j1-lee
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.