Counting lines, words, and characters within a text file using Python

Posted on

Question :

Counting lines, words, and characters within a text file using Python

I’m having a bit of a rough time laying out how I would count certain elements within a text file using Python. I’m a few months into Python and I’m familiar with the following functions;

  • raw_input
  • open
  • split
  • len
  • print
  • rsplit()

Here’s my code so far:

fname = "feed.txt"
fname = open('feed.txt', 'r')

num_lines = 0
num_words = 0
num_chars = 0

for line in feed:
    lines = line.split('n')

At this point I’m not sure what to do next. I feel the most logical way to approach it would be to first count the lines, count the words within each line, and then count the number of characters within each word. But one of the issues I ran into was trying to perform all of the necessary functions at once, without having to re-open the file to perform each function seperately.

Answer #1:

Try this:

fname = "feed.txt"

num_lines = 0
num_words = 0
num_chars = 0

with open(fname, 'r') as f:
    for line in f:
        words = line.split()

        num_lines += 1
        num_words += len(words)
        num_chars += len(line)

Back to your code:

fname = "feed.txt"
fname = open('feed.txt', 'r')

what’s the point of this? fname is a string first and then a file object. You don’t really use the string defined in the first line and you should use one variable for one thing only: either a string or a file object.

for line in feed:
    lines = line.split('n')

line is one line from the file. It does not make sense to split('n') it.

Answered By: eumiro

Answer #2:

Functions that might be helpful:

  • open("file").read() which reads the contents of the whole file at once
  • 'string'.splitlines() which separates lines from each other (and discards empty lines)

By using len() and those functions you could accomplish what you’re doing.

Answered By: kynnysmatto

Answer #3:

fname = "feed.txt"
feed = open(fname, 'r')

num_lines = len(feed.splitlines())
num_words = 0
num_chars = 0

for line in lines:
    num_words += len(line.split())
Answered By: Stephen Paulger

Answer #4:

file__IO = input('nEnter file name here to analize with path:: ')
with open(file__IO, 'r') as f:
    data =
    line = data.splitlines()
    words = data.split()
    spaces = data.split(" ")
    charc = (len(data) - len(spaces))

    print('n Line number ::', len(line), 'n Words number ::', len(words), 'n Spaces ::', len(spaces), 'n Charecters ::', (len(data)-len(spaces)))

I tried this code & it works as expected.

Answered By: Ozzius

Answer #5:

One of the way I like is this one , but may be good for small files

with open(fileName,'r') as content_file:
    content =
    lineCount = len(re.split("n",content))
    words = re.split("W+",content.lower())

To count words, there is two way, if you don’t care about repetition you can just do

words_count = len(words)

if you want the counts of each word you can just do

import collections
words_count = collections.Counter(words) #Count the occurrence of each word
Answered By: sirus

Leave a Reply

Your email address will not be published. Required fields are marked *