简体   繁体   中英

Counting occurrences of a word in a file

I'm new to Python and I have no idea what to do. So this is the question:

Write a function that takes a filename and a word (or, if no word is given, assumes the word is “hello”), and returns an integer representing how many times that word appears in the file, except instances in the first line of the file don't count.

Call the function with two args, a filename that must be in path and the word to count inside the file. If you don't pass the word the function assumes the default word "hello".

def wordcount(filename, word="hello"):
    # Open the file name
    with open(filename) as file:
        # Skip first line
        next(file)  
        # read the file as a string except the first line and count the occurrences 
        return file.read().count(word)

Count method returns the number of occurrences of the substring in the given string. Also u can save the first line x = next(file) if you what to use it later.

Call the function and print the result with print(wordcount("sample.txt", "repeat")) to count how many times the word 'repeat' appears in the file.

The sample.txt contains:

Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !
Hello ! My name is João, i will repeat this !

The result must be 4:)

from pathlib import Path

def count(filename: str, word = "hello"):
    file = Path(filename)
        
    text = file.read_text()
    lines_excluding_first = text.split("\n")[1:]
    
    counts = sum([sum(list(map(lambda y: 1 if word == y else 0, x.split(" ")))) for x in lines_excluding_first])
    
    
    return counts

Example: say you have a txt file like:

sample.txt
----------

this is the first line so this dose not count!
hello!! this is a sample text.
text which will be used as sample.
if nothing no word specified hellow is returned!
it will return the count.
print(count("sample.txt")) 
## output: 2

EDIT:

I have made a small correction in the code now "hellow!!" and "hellow" are two separate words. Words are separated by blank spaces and than checked for equality. Therefore "hellow" and "hellow." are also different!

As per the request here is how it will look on repl.it :

make a sample.txt first: 样本.txt main.py looks like: 主程序 you can see the output as 2 for default "hello"
[output is case sensitive ie Hello and hello are not the same]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM