Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the number of characters in a file using Python

Tags:

Here is the question:

I have a file with these words:

hey how are you I am fine and you Yes I am fine 

And it is asked to find the number of words, lines and characters.

Below is my program, but the number of counts for the characters without space is not correct.

The number of words is correct and the number of line is correct. What is the mistake in the same loop?

fname = input("Enter the name of the file:") infile = open(fname, 'r') lines = 0 words = 0 characters = 0 for line in infile:     wordslist = line.split()     lines = lines + 1     words = words + len(wordslist)     characters = characters + len(line) print(lines) print(words) print(characters) 

The output is:

lines=3(Correct) words=13(correct) characters=47 

I've looked on the site with multiple answers and I am confused because I didn't learn some other functions in Python. How do I correct the code as simple and basic as it is in the loop I've done?

Whereas the number of characters without space is 35 and with space is 45. If possible, I want to find the number of characters without space. Even if someone know the loop for the number of characters with space that's fine.

like image 710
S.Soopra Avatar asked Jan 06 '17 11:01

S.Soopra


People also ask

How do I find the number of characters in a file?

To get exact character count of string, use printf, as opposed to echo, cat, or running wc -c directly on a file, because using echo, cat, etc will count a newline character, which will give you the amount of characters including the newline character.

How do I get the length of a file in Python?

The python os module has stat() function where we can pass the file name as argument. This function returns a tuple structure that contains the file information. We can then get its st_size property to get the file size in bytes.

How do you count spaces in a file in Python?

A loop is used to count spaces in a text file. if condition ( char. isspace()) to test all the condition, if it returns True then the count will be incremented by 1. After testing all the character's loop will return False and terminate itself.


Video Answer


1 Answers

Sum up the length of all words in a line:

characters += sum(len(word) for word in wordslist) 

The whole program:

with open('my_words.txt') as infile:     lines=0     words=0     characters=0     for line in infile:         wordslist=line.split()         lines=lines+1         words=words+len(wordslist)         characters += sum(len(word) for word in wordslist) print(lines) print(words) print(characters) 

Output:

3 13 35 

This:

(len(word) for word in wordslist) 

is a generator expression. It is essentially a loop in one line that produces the length of each word. We feed these lengths directly to sum:

sum(len(word) for word in wordslist) 

Improved version

This version takes advantage of enumerate, so you save two lines of code, while keeping the readability:

with open('my_words.txt') as infile:     words = 0     characters = 0     for lineno, line in enumerate(infile, 1):         wordslist = line.split()         words += len(wordslist)         characters += sum(len(word) for word in wordslist)  print(lineno) print(words) print(characters) 

This line:

with open('my_words.txt') as infile: 

opens the file with the promise to close it as soon as you leave indentation. It is always good practice to close file after your are done using it.

like image 197
Mike Müller Avatar answered Sep 21 '22 09:09

Mike Müller