mthe25 mthe25 - 3 months ago 6x
Python Question

Python basic program to count the letters in a file

I'm writing a program in Python for an online class in order to find the frequency of letters in a file. Thing is I keep getting spaces included in the final result too. How can I omit them? Here's my code:

import string
name = raw_input('Enter a file name: ')
fhandle = open(name)
counts = dict()
for line in fhandle:
line = line.strip()
line = line.translate(None,string.punctuation)
line = line.lower()
letters = list(line)
for letter in letters:
lst = list()
for letter,count in counts.items():
for count,letter in lst:
print count,letter


string.punctuation contains !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ and no whitespace characters.

You should change your call to translate() to the following:


Type help(string) in the python interpreter for more information.