malafat44
malafat44

Reputation: 45

appending data from a text file into a list in python

i am new to python and i need to read numbers from a text file and pass those in order into a list here is what i came up with :

liste = []
with open("MDATX3.014") as fa:
    lines = fa.readlines()
    for line in lines:
        words = line.split()
        for word in words:
            liste.append(word)
            print(liste)

and i had the result like this enter image description here

Upvotes: 3

Views: 803

Answers (1)

blhsing
blhsing

Reputation: 106425

You should print(liste) outside the loop if you don't want to see its intermediate values while it is being appended with new values:

liste = []
with open("MDATX3.014") as fa:
    lines = fa.readlines()
    for line in lines:
        words = line.split()
        for word in words:
            liste.append(word)
print(liste)

Upvotes: 2

Related Questions