Can this be removed? "Index: [], Empty DataFrame" and correctly read in

Viewed 34

I'm currently reading in .txt files from a folder into an array, then appending each array to a list.

path_M = r'FYP_Datasets/Large_Movie_dataset/aclImdb/train/pos//'
all_files = glob.glob(path_M + "*.txt")

for filename in all_files:
    temp = np.loadtxt(filename,dtype=str,encoding="UTF-8")
    li.append(temp)
print(li)

When I print the list, it is printing out "Index: [], Empty DataFrame"

Is there a way to remove this? Ideally, I want the text file read into a list with each word separated. Then that array put into one overall list. i.e. [ ["I", "am", "here"] ["You", "Are", "Here"] ]

enter image description here

1 Answers

Use an if condition when printing out the elements of the li list to only print out the numpy array that are not empty:

for df in li:
    if df.size:
        print(df)

To eliminate the empty numpy arrays from the li list, you can use a list comprehension:

li = [df for df in li if df.size]

Or you can avoid appending the empty numpy array to the list in the first place:

for filename in all_files:
    df = np.loadtxt(filename,dtype=str,encoding="UTF-8")
    if df.size:
        li.append(df)
Related