[Python] How to read a file line by line into array

Hey đŸ˜‰

How to read a file line by line into array in Python:

f = open(“filename.txt”, “r” )
lines = f.readlines()
f.close()

#Remove /n at the end of the line
lines = (line.rstrip(‘n’) for line in open(“filename.txt”))

#Add lines to our Array Object
array = []
for line in lines:
    array.append(line)

Now you can access each line this way: array[0] (Line 1) … array[1] (Line 2) and so on! đŸ˜‰

Enjoy