'Write a function to take a text file and print the number of lines, words, and characters in the file
I am currently struggling with a question related to Python File I/O. The question I that I fail to be able to complete is this:
Write a function stats() that takes one input argument: the
name of a text file. The function should print, on the screen, the
number of lines, words, and characters in the file; your function
should open the file only once.
I must also eliminate all puncuation to properly get all of the words.The function should print like so:
>>>stats('example.txt')
line count: 3
word count: 20
character count: 98
Solution 1:[1]
Please review the Python documentation about I/O here, Built-in Functions here, and Common string
operations here.
There are many ways to get this done. With a quick go at it, the following should get the job done based on your requirements. The split
function will eliminate spaces when it converts each line to a list
.
def GetFileCounts(in_file):
char_count = 0
word_count = 0
line_count = 0
with open(in_file) as read_file:
for line in read_file:
line_count += 1
char_count += len(line)
word_count += len(line.split(' '))
print "line count: {0}\nword count: {1}\ncharacter count: {2}".format(line_count, word_count, char_count)
You may want to refine your requirements defintion a bit more as there are some subtle things that can change the output:
- What is your definition of a character?
- only letters?
- letters, spaces, in word punctuation (e.g. hyphens), end of lines, etc.?
Solution 2:[2]
As far as your question goes,you can achieve it by:
fname = "inputfile.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)
There are lots of free tutorial online for python. Please refer those throughly. You can find reference books here
Solution 3:[3]
Are you open to third party libs?
Have a look at: https://textblob.readthedocs.org/en/dev/
It has all of the features you want implemented pretty well.
Solution 4:[4]
@nagato , i think yout with num_chars += len(line) you come all from the file , character's and blank space , but thank for your code , i can my to do...
My...
def char_freq_table():
with open('/home/.....', 'r') as f:
line = 0
word = 0
character = 0
for i in f:
line = len(list('f'))
word += len(i.split())
character = i.replace(" ", "") #lose the blank space
print "line count: ", line
print "word count: ", word
print "character count: ", len(character) # length all character
char_freq_table()
Solution 5:[5]
filename = 'Inputfile.txt'
def filecounts(filename):
try:
f= open(filename)
except IOError:
print("Unable to open the file for reading %s: %s" % (filename))
text = f.readlines()
f.close()
linecount = 0
wordcount = 0
lettercount = 0
for i in text:
linecount += 1
lettercount += len(i)
wordcount += len(i.split(' '))
return lettercount, wordcount, linecount
print ("Problem 1: letters: %d, words: %d, lines: %d" % (filecounts(filename)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Community |
Solution 3 | labheshr |
Solution 4 | |
Solution 5 | S.B |