0

I've been trying to get this line of code to work. I'm new to python so I'm not sure how many issues there are. Jupyter only gives one error at a time. Can't figure out the exact issue. I keep getting a syntax error for line 16 "print Counter"

I'm trying to import text from a file and count how many times each letter in the alphabet occurs. Then, I'm trying to graph the letter count in a histogram. Any thoughts?

import string
import sys
import matplotlib.pyplot as plt
%matplotlib inline 
from collections import Counter
from string import ascii_lowercase

def get_counts(fname):
# this function takes the name of a file as input parameter, fname
# it then reads the file into a string variable
# then proceeds to process the string for the count of letters
# answer should be returned in a list of 26 numbers

    with open('ra.docx') as f:
        print Counter(letter for line in f 
            for letter in line.lower() 
            if letter in ascii_lowercase)
        

def draw(counts):
# this function takes as input the "counts" list
# the function then proceeds to draw the histogram using matplotlib


    count = [1, 5, 10, 15, 20, 25, 30, 35, 40,45, 50, 55, 60]
    letters =["A","B","C","D","E","F","G","H","I","J", "K", "L", "M", "N","O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    xaxis = range(len(letters))
    plt.figure(figsize=(12,8)) 
    plt.title("RA Text Histogram ")
    plt.xlabel("Letters")
    plt.ylabel("Count")
    plt.bar(xaxis, count, width=0.5)
    plt.xticks(xaxis, letters)
    for a,b in zip(xaxis, fall_enrollments):
    plt.text(a, b+20, str(b), horizontalalignment='center', verticalalignment='center')
    plt.show()

def main():
  #counts = get_counts(sys.argv[1])
  counts = get_counts("ra.txt")
  #print(counts)
  draw(counts)

main()
Twigy
  • 1

1 Answers1

0

print as a statement (i.e. print followed by a space and a string) is Python 2 syntax. If you are using Python 3 (there is a good chance by now ...), you have to use the print() function:

print(Counter(letter for line in f 
              for letter in line.lower() 
              if letter in ascii_lowercase))

Since you already imported sys, what does

sys.version

return?

Nils L.
  • 676
  • 5
  • 6
  • Thanks - That's literally all it needed on the count side. But the histogram still isn't displaying content. Even though it is running. It did ask me to indent the plt.text line, but otherwise, I'm no longer getting any errors or outputs. I also don't see a sys.version in my code. – Twigy Jul 16 '20 at 16:50
  • OK, some other problems I can immediately spot are that in your `main()` function you call `get_counts()` with `"ra.txt"` while in the definition of `get_counts()`, you hard-coded the filename `"ra.docx"`. Is the code you show here really the code that you execute? Otherwise, the variable `counts` seems to be unused in the definition of `draw()`. There, you hard-coded the variable `count`. Was that for debugging? I do not really understand your code. – Nils L. Jul 16 '20 at 16:58
  • Also, have a look at this: https://stackoverflow.com/questions/52474028/letter-frequencies-plot-a-histogram-ordering-the-values-python I think that is what you are trying to accomplish, right? – Nils L. Jul 16 '20 at 17:01
  • There were a couple of typos in there. I fix the .txt issue. I'm trying to pull the input from the draw function so that the data I'm importing goes into the histogram. Perhaps I need to include the counts variable in the draw section? – Twigy Jul 16 '20 at 17:24