Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of lines in a txt file with Python excluding blank lines

Tags:

python

I wish to count the number of lines in a .txt file which looks something like this:

apple
orange
pear

hippo
donkey

Where there are blank lines used to separate blocks. The result I'm looking for, based on the above sample, is five (lines).

How can I achieve this?

As a bonus, it would be nice to know how many blocks/paragraphs there are. So, based on the above example, that would be two blocks.

like image 436
samiles Avatar asked May 20 '12 12:05

samiles


2 Answers

Considering the blank lines will only contain the new line character, it would be pretty faster to avoid calling str.strip which creates a new string but instead to check if the line contains only spaces using str.isspace and then skip it:

with open('data.txt') as f:
    non_blank_lines = sum(not line.isspace() for line in f)

Demo:

from io import StringIO

s = '''apple
orange
pear

hippo
donkey'''

non_blank_lines = sum(not line.isspace() for line in StringIO(s)))
# 5

You can further use str.isspace with itertools.groupby to count the number of contiguous lines/blocks in the file:

from itertools import groupby

no_paragraphs = sum(k for k, _ in groupby(StringIO(s), lambda x: not x.isspace()))
print(no_paragraphs)
# 2
like image 101
Moses Koledoye Avatar answered Oct 21 '22 02:10

Moses Koledoye


sum([1 for i in open("file_name","r").readlines() if i.strip()])
like image 38
Revanth Avatar answered Oct 21 '22 01:10

Revanth