Jan-16-2019, 02:25 AM
(This post was last modified: Jan-16-2019, 02:26 AM by Drone4four.)
Hi @snippsat! Thanks for your reply.
I am very much looking forward to seeing a working demo on my system of both
You said:
You are right that both Debian, Fedora and most of their derivatives still don’t have Python 3 installed by default. It’s still Python 2 for them users. But I’m running Manjaro stable. Here is the version of Python in my shell:
The output is still empty. After enter these commands, my unix shell just prompts me for my next command.
For the record, here are my two scripts verbatim:
pycounter.py:
Edit: It's also worth pointing out that I have triple-checked that the text file present in my directory and the text file as referenced in both scripts match. The name of the text file in all locations is
I am very much looking forward to seeing a working demo on my system of both
f-string
formatting and the words counted with variations of punctuation but all my output is still blank.You said:
(Jan-15-2019, 02:40 AM)snippsat Wrote:(Jan-15-2019, 02:00 AM)Drone4four Wrote: When I run $ python pycounter.py, nothing happens. Why?You need to use$ python3 pycounter.py
to use Python 3 if you have default Linux OS setup.
You are right that both Debian, Fedora and most of their derivatives still don’t have Python 3 installed by default. It’s still Python 2 for them users. But I’m running Manjaro stable. Here is the version of Python in my shell:
Output:$ python --version
Python 3.7.1
I attempt to run my two scripts using: $ python pycounter.py
and $ python snippsat.py
(I’ve named your suggested alternate script after your forum alias.)The output is still empty. After enter these commands, my unix shell just prompts me for my next command.
For the record, here are my two scripts verbatim:
pycounter.py:
#!/usr/bin/env python # encoding: utf-8 """ alice_file.py Created by Jason Elbourne on 2011-12-29. Copyright (c) 2011 Jason Elbourne. All rights reserved. """ import operator ## Get each word - Turn to Lower case (.lower()) ## Count Duplicates of words ## Dictionary {word:count,word2:count2} ## Sort this based on most used word ## Print the Top 20 Words def rank_words(f): """ Takes in a file, then ranks all the words within the file Args: a file Return: A sorted list of tuples """ word_dict = {} # Start with empty python Dictionary words = [] # Start with empty python List for line in f: list_of_words = line.split() for w in list_of_words: words.append(w.lower()) # Add Word to List for word in words: if word in word_dict: word_dict[word] += 1 # Incr. value in Dict. else: word_dict[word] = 1 # Add word and value to Dict. # This will sort the dictionary and return a list of Tuples return sorted(word_dict.items(), reverse=True, \ key=operator.itemgetter(1)) def main(): # Files f = open('Alice.txt') ranked_words_list = rank_words(f) f.close() # Print the results for w in list(ranked_words_list[:10]): print(w[0],"---", w[1]) if __name__ == '__main__': main()snippsat.py:
#!/usr/bin/env python """ This script was provided by snippsat on python-forum.io. Here is the URL: https://python-forum.io/Thread-Python-word-counter-and-ranker """ from collections import Counter import re with open('Alice.txt') as f: text = f.read().lower() 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}')I'm at a loss here. Would anyone care to jump in here and save the day?
Edit: It's also worth pointing out that I have triple-checked that the text file present in my directory and the text file as referenced in both scripts match. The name of the text file in all locations is
Alice.txt
.