Python character and word counts
Question: I'm a beginner with python and would like to know how to use two txt files to
count the characters as well as counter the 10 most common characters. also
how to convert all characters in the file to lower case and eliminate all
characters other than a-z

here's what i've tried and had no luck with:

    
    
    from string import ascii_lowercase
    from collections import Counter
    
    with open ('document1.txt' , 'document2.txt') as f:
        print Counter(letter for line in f
                        for letter in line.lower()
                        if letter in ascii_lowercase)
    


Answer: Here is a simple example. You can adapt this code to fit your needs

    
    
    from string import ascii_lowercase
    from collections import Counter
    
    with open('file1.txt', 'r') as file1data: #opening an reading file one
        file1 = file1data.read().lower() #convert the entire file contents to lower
    
    with open('file2.txt', 'r') as file2data: #opening an reading file two
        file2 = file2data.read().lower() 
    
    #The contents of both file 1 and 2 are stored in fil1 and file2 variables
    #Examples of how to work with one file repeat for two files
    file1_list = []
    for ch in file1:
        if ch in ascii_lowercase: #makes sure only lowercase alphabet is appended.  All Non alphabet characters are removed
            file1_list.append(ch)
        elif ch in [" ", ".", ",", "'"]: #remove this elif block is you just want the letters
            file1_list.append(ch) #make sure basic punctionation is kept
    
    print "".join(file1_list) #this line is not needed. Just to show what the text looks like now
    print Counter(file1_list).most_common(10) #prints the top ten
    print Counter(file1_list) #prints the number of characters and how many times they repeat
    

Now that you've reviewed that mess above and have an idea of what each line is
doing, here is a cleaner version, that does what you were looking for.

    
    
    from string import ascii_lowercase
    from collections import Counter
    
    with open('file1.txt', 'r') as file1data: 
        file1 = file1data.read().lower()
    
    with open('file2.txt', 'r') as file2data: 
        file2 = file2data.read().lower() 
    
    file1_list = []
    for ch in file1:
        if ch in ascii_lowercase: 
            file1_list.append(ch)
    
    file2_list = []
    for ch in file2:
        if ch in ascii_lowercase: 
            file2_list.append(ch)
    
    
    
    all_counter = Counter(file1_list + file2_list) 
    top_ten_counter = Counter(file1_list + file2_list).most_common(10) 
    
    print sorted(all_counter.items()) 
    print sorted(top_ten_counter)
    

