with open("text.txt") as f:
for line in f:
line.isalpha()
False
File has only one line and contents are: "abc"
with open("text.txt") as f:
for line in f:
line.isalpha()
False
File has only one line and contents are: "abc"
I think this is because there is a space after the "abc" content
As far as I know file lines are usually terminated by newline character \n
which is the answer why isalpha()
returns false.
As the others pointed out, it must be for some other characters in the file; likely either "\n" for line termination, or some others.
In brief, you want to remove those characters. Try:
line.strip().isalpha()
Full explanation below.
Load data:
with open("text.txt") as f:
for line in f:
line.isalpha()
The output of line is:
>>> line
'abc\n'
And of course the result of isalpha()
is false:
>>> print(line.isalpha())
False
However, removing the \n
you obtain the correct result:
>>> line.strip()
'abc'
>>> line.strip.isalpha()
True
(To troubleshoot this, you may want to just output the line in the interpreter, without print
statements, otherwise you won't see special characters as '\n'
)