Q:

Python list isalpha not working

file1=open(textfile1.txt,"r")

list=[]

for line in file1:

    list.append(line) #turn textfile into a list

list2=[line for line in list if line.isalpha()] #approach 1

list3=[]

for line in file1: #approach 2

    if line.isalpha():list3.append(line)

textfile1 is like this:
car

tru5ck99

letter

space72 

and so on..
I have to somehow collect the lines of text with only text in them, no numbers.
List2 and List3 are just two different approaches, but neither seem to put anything into the new list.
Thank you!

A:

listalpha = []

with open('testfile.txt') as f:
for line in f:
    x = line.strip()
    if x.isalpha():
        listalpha.append(x)

print(listalpha)

Result = ['car', 'letter']
Like Patrick Haugh said, you need to use strip() to remove the \n at the end of each word

