0

I am opening trying to create a function that opens a .txt file and counts the words that have the same length as the number specified by the user.
The .txt file is:

This is a random text document. How many words have a length of one? 
How many words have the length three?  We have the power to figure it out! 
Is a function capable of doing this?

I'm able to open and read the file, but I am unable to exclude punctuation and find the length of each word.

def samplePractice(number):
    fin = open('sample.txt', 'r')
    lstLines = fin.readlines()
    fin.close

    count = 0

    for words in lstLines:
        words = words.split()

    for i in words:
        if len(i) == number:
            count += 1
    return count
jackrabbit
  • 87
  • 9
  • 1
    What do you mean you are unable to do it? What have you tried to remove punctuation? Is there an error you encountered? – razdi May 05 '19 at 23:25
  • Initially I tried splitting the lines like split('[.,?!]') but it just gave me an output of 0 – jackrabbit May 05 '19 at 23:28
  • Possible duplicate of [How to delete a character from a string using Python](https://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python) – Boris Verkhovskiy May 05 '19 at 23:43

3 Answers3

0

You can try using the replace() on the string and pass in the desired punctuation and replace it with an empty string("").

It would look something like this:

puncstr = "Hello!"

nopuncstr = puncstr.replace(".", "").replace("?", "").replace("!", "")
PiAreSquared
  • 324
  • 3
  • 10
0

I have written a sample code to remove punctuations and to count the number of words. Modify according to your requirement.

    import re
    fin = """This is a random text document. How many words have a length of one? How many words have the length three?  We have the power to figure it out! Is a function capable of doing this?"""
    fin = re.sub(r'[^\w\s]','',fin)
    print(len(fin.split()))

The above code prints the number of words. Hope this helps!!

KiranMayee Maddi
  • 301
  • 1
  • 12
0

instead of cascading replace() just use strip() a one time call

Edit: a cleaner version

pl = '?!."\'' # punctuation list

def samplePractice(number):
    with open('sample.txt', 'r') as fin:
        words = fin.read().split()

    # clean words
    words = [w.strip(pl) for w in words]

    count = 0
    for word in words:
        if len(word) == number:
            print(word, end=', ')
            count += 1

    return count

result = samplePractice(4)
print('\nResult:', result)

output:

This, text, many, have, many, have, have, this, 
Result: 8

your code is almost ok, it just the second for block in wrong position

pl = '?!."\'' # punctuation list

def samplePractice(number):
    fin = open('sample.txt', 'r')
    lstLines = fin.readlines()
    fin.close

    count = 0

    for words in lstLines:
        words = words.split()

        for i in words:
            i = i.strip(pl)  # clean the word by strip
            if len(i) == number:
                count += 1
    return count

result = samplePractice(4)
print(result)

output:

8
Mahmoud Elshahat
  • 1,873
  • 10
  • 24
  • @Mahmoud_Elshahat the above code doesn't seem to give me the right output. In my editor, if I input samplePractice(3) I expect the output of 6 but I only get 5. Any idea why? – jackrabbit May 06 '19 at 00:19
  • @jackrabbit just try my last edited answer, it gave me 6 as expected `How, one,How, the, the, out, Result: 6` – Mahmoud Elshahat May 06 '19 at 00:21