简体   繁体   中英

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:
        counts[letter]=counts.get(letter,0)+1
 lst = list()
    for letter,count in counts.items():
        lst.append((count,letter))
lst.sort(reverse=True)
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:

line.translate(None,string.punctuation+string.whitespace+string.digits)

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

If you don't want to print the letter if it is a blank space (and you don't want to change anything else in your code), then you can add one if statement in the last for loop:

for count,letter in lst:
    if letter != ' ':
        print count,letter

An elegant way to do this is to just use isalpha(). See line 11:

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:
        if letter.isalpha() == True:
            counts[letter]=counts.get(letter,0)+1
    lst = list()
    for letter,count in counts.items():
        lst.append((count,letter))
lst.sort(reverse=True)
for count,letter in lst:
    print count,letter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM