Login
Remember
Register
Questions
Unanswered
Categories
Ask a Question
Quiz
Ask a Question
Python - How to check if a line read from file is empty?
+2
votes
asked
May 31, 2016
in
Programming Languages
by
praveen
(
72.0k
points)
I have an input text file with several records. Some of the records might be empty. How can I skip such records in python? I tried to use len(line), but it didn't work.
python
empty
line
Please
log in
or
register
to add a comment.
Please
log in
or
register
to answer this question.
1
Answer
+2
votes
answered
May 31, 2016
by
praveen
(
72.0k
points)
selected
Mar 9
by
pkumar81
Best answer
You can use split() function to check the empty line. For example, see the following code.
with open('badlinks.txt', 'r') as bl:
for line in bl:
if line.strip() == '':
continue
else:
id, skey = line.split('||||')
print id
Please
log in
or
register
to add a comment.
Related questions
+2
votes
1
answer
Python : How to check if a line is an empty line
asked
Apr 20, 2018
in
Programming Languages
by
pythonuser
(
77.0k
points)
file
python
record
line
+5
votes
1
answer
How to check if a Pandas DataFrame is empty?
asked
Dec 27, 2021
in
Programming Languages
by
pythonuser
(
77.0k
points)
pandas
empty
dataframe
+2
votes
1
answer
Python - How to check an empty dictionary ?
asked
Jun 18, 2016
in
Programming Languages
by
praveen
(
72.0k
points)
python
dictionary
empty
+2
votes
1
answer
Python : How to read the newly added line from a file that is dynamic
asked
Apr 20, 2018
in
Programming Languages
by
pythonuser
(
77.0k
points)
python
file
dynamic
+2
votes
1
answer
How to check if a file exists in a directory using Python
asked
Oct 18, 2019
in
Programming Languages
by
pythonuser
(
77.0k
points)
python
directory
file
Categories
All categories
Programming Languages
(731)
Machine Learning
(34)
Operating Systems
(128)
Databases
(97)
Web & Google
(90)
CMS Tips
(34)
General IT Tips
(89)
GK Quiz QA
(81)
...