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()