Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2017
    Posts
    26
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Help with rstrip needed

    Hey. I am having problems with the following code:

    PHP Code:

    from collections import Counter
    import re

    wordcount 
    Counter()
    with open("moby_dick.txt","r") as f:
        for 
    line in f:
            
    wordcount.update(line.rstrip('?:!,.;').split())
    for 
    k,v in wordcount.most_common():
        print(
    kv
    Which does everything it should except strip off the trailing punctuation. I also think I need make the drop the words down to lowercase as well since:

    Code:
    C:\Python Code>python testCounter.py > text.txt
    C:\Python Code>type text.txt | find /c /v ""
    32551

    My line count is insane. Any help is appreciated. Thanks.

  2. #2
    New Coder
    Join Date
    Feb 2017
    Posts
    26
    Thanks
    7
    Thanked 2 Times in 2 Posts
    After some research on strip & rstrip's behavior, I tried this:

    PHP Code:

    from collections import Counter
    import string

    wordcount 
    Counter()
    with open("moby_dick.txt","r") as f:
        for 
    line in f:
            
    clean "".join((char for char in line if char not in string.punctuation))
            
    wordcount.update(clean.lower().split())
    for 
    k,v in wordcount.most_common():
        print(
    k
    And now:

    Code:
    C:\Python Code>python testCounter.py > output.log
    
    C:\Python Code>type output.log | find /c /v ""
    19695
    I'm still having issues with in-dwelling punctuation between words, but this should be sufficient for class. Thanks

    J.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •