Help with opening a .txt file

I have a project where I need to write a program that counts the number of recurring letters from an imported text file, and then prints their frequencies from high to low. I'm already stuck because I can't figure out how to import a text file. Does the file need to be saved anywhere specific in order for it to be called into the code? Any help would be greatly appreciated!!

EDIT: ok this is the code that I have so far...

file = open('gettysburgaddress.txt')
text = file.read()
file.close()

newString = ''

char_list= text.lower()
letters = "abcdefghijklmnopqrstuvwxyz"
for char in char_list:
    if char in letters:
        newString += char
n = len(char_list)

char_total = {}
for c in newString:
    if c not in char_total:
        char_total[c] = 1
    else:
        char_total[c] += 1

I know its not very pretty, but I'm just a beginner. Now I can't figure out how to do the output. I've got it to where it will print the number of times a letter appears, but it needs to be in frequencies and from highest to lowest. I've been trying to understand how to pretty print, so if anyone has any advice that would be awesome

Posted Dec 2, 2011, 4:13 AM UTC · edited

76
Loading comments…