In [1]:
#Problem: Voters in Florida
#Group members: S. Wang, Y. Ye, M. Staton, W. Wang

In [2]:
#access regex functions
import re

In [3]:
#make a handle on the html file
html_handle = open("FloridaVoters.html", "r")

#read in one line at a time
new_line = html_handle.readline()

#make an empty list to store all lines
records = []

#it turns out the desired tag is "<td>". This line will retrieve all the lines
#inside the "<td>" tag
while len(new_line) != 0:
    
    if new_line.startswith("<td>"):
        
        records.append(str(new_line.strip("<td></td>\r\n")))
    
    new_line = html_handle.readline()
    
#close the handle to end the process
html_handle.close()

In [4]:
#create an empty list to store the data
clean_list = []

#every state takes 6 places in the list, so iterate over the list by 6 every time
for i in range(0, len(records), 6):
    
    county = records[i]
    
    rep_count = int(records[i+1].replace(",",""))
    dem_count = int(records[i+2].replace(",",""))
    
    clean_list.append({"county": county, "rep": rep_count, "dem": dem_count})

#define a helper function to sort the list by democratic votes
def get_dem(dict):
    return dict["dem"]

In [5]:
#sort the list by democratic votes
sorted_list = sorted(clean_list, key=get_dem)

#print the sorted list line by line
for elem in sorted_list:
    print "{:<15}{:>8}{:>8}".format(elem["county"], elem["rep"], elem["dem"])


LAFAYETTE          1373    2672
GLADES             2190    3110
LIBERTY             720    3372
UNION              2752    3579
GILCHRIST          5789    3652
FRANKLIN           2234    4319
HOLMES             5282    4434
GULF               4234    4521
HARDEE             4859    4702
HAMILTON           2154    4796
DIXIE              3314    4839
CALHOUN            2201    5324
WASHINGTON         7101    5687
JEFFERSON          2636    5802
BAKER              6963    5813
BRADFORD           6878    6533
TAYLOR             3950    6915
MADISON            2992    7158
DESOTO             4870    7181
OKEECHOBEE         7755    7756
HENDRY             5862    7999
WAKULLA            7374    8889
LEVY              11665    9509
WALTON            25609   10013
SUWANNEE          10745   11126
NASSAU            32958   14013
COLUMBIA          15790   14797
JACKSON            9626   15706
MONROE            20602   17614
HIGHLANDS         27100   19997
PUTNAM            17067   20606
GADSDEN            4372   22161
SUMTER            47158   22977
FLAGLER           30047   24734
OKALOOSA          75154   25172
SANTA ROSA        73627   26114
MARTIN            53800   27358
INDIAN RIVER      47794   28204
CITRUS            46373   30440
BAY               57456   30733
CLAY              76584   31285
CHARLOTTE         54421   35602
ST. JOHNS         88385   39531
HERNANDO          51254   42499
COLLIER          100167   45511
LAKE              93604   67237
MANATEE           96063   67926
ESCAMBIA          90265   70180
OSCEOLA           44594   75657
ST. LUCIE         59626   76163
MARION            97306   76268
ALACHUA           47329   77996
SARASOTA         125872   89711
SEMINOLE         107833   91686
LEON              54554  103140
PASCO            125305  104324
LEE              180718  114633
VOLUSIA          121402  124136
BREVARD          167129  127435
POLK             140619  143799
PINELLAS         223077  221968
DUVAL            210195  229501
ORANGE           206174  303458
HILLSBOROUGH     257436  314265
PALM BEACH       245452  367236
MIAMI-DADE       362161  539367
BROWARD          249762  566185
Total           4377713 4637026