Python Forum
Extending my text file word count ranker and calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extending my text file word count ranker and calculator
#4
Thank you @stullis for the clarification. I’ve moved lines 21 and 22 into my main() function. It runs beautifully as expected. I’ll keep in mind for next time to run all the required code in my program inside main() and I’ll avoid using a return operation there as well.

For what it’s worth, here is the updated working script:

from collections import Counter
import re
 
def word_count(text):
    wordlist = text.split()
    print("A total of " + str(len(wordlist)) + " words can be found inside this text file.")

def rank_words(text):
    words = re.findall('\w+', text)
    top_10 = Counter(words).most_common(10)
    for word,count in top_10:
        print(f'{word:<4} {"-->":^4} {count:>4}')

def main():
    with open('Alice.txt') as f:
        text = f.read().lower()
    word_count(text)
    rank_words(text)
        
if __name__ == '__main__':
    main()
    pass
And the output:
Output:
$ python with_word_count.py A total of 29465 words can be found inside this text file. the --> 1818 and --> 940 to --> 809 a --> 690 of --> 631 it --> 610 she --> 553 i --> 543 you --> 481 said --> 462
@snippsat: You are right that f-string formatting is more readable and more concise. It’s cleaner and less ugly. When I wrote that string concatenation line initially, I just used whatever was most obvious. On my lunch break tomorrow I’ll read a tutorial I found called “Python 3's f-Strings: An Improved String Formatting Syntax (Guide)

@snippsat: I like the polish and I appreciate the detailed explanation about how you need if __name__ == '__main__': if you are importing a script as a module in the interpreter on the fly. I’ve looked up if __name__ == '__main__': on Google and there are many tutorials and guides. The most upvoted question and subsequent answers on SO explaining and describing this mechanism in Python I find to be overly confusing right now. I suppose once I learn more about classes and with more general experience with Python, the logic and syntax of if __name__ == '__main__': will make more sense. @snippsat: Compared to Sackoverflow, your explanation is easier to understand because it's in the context of my script. Thank you.
Reply


Messages In This Thread
RE: Extending my Python word count ranker and calculator - by Drone4four - Jan-19-2019, 04:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 3,555 Oct-17-2023, 06:03 PM
Last Post: Devan
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,151 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Row Count and coloumn count Yegor123 4 1,361 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,641 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  find some word in text list file and a bit change to them RolanRoll 3 1,567 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  python-docx regex: replace any word in docx text Tmagpy 4 2,267 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,726 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,047 Feb-11-2022, 12:38 AM
Last Post: atomxkai
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,533 Aug-12-2021, 04:25 PM
Last Post: palladium
  all i want to do is count the lines in each file Skaperen 13 4,898 May-23-2021, 11:24 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020