# Reading a file, and putting the data into a dictionary
# Then reading the file again, and adding more data to the
# dictionary. Finally, dumps all the data from the dictionary
# to an output file that could be read by Many Eyes.

# Open the input data file for reading
stateFile = open("StatePopulations.txt","r")

# Begin with the empty dictionary
popDict = {}
for line in stateFile:
    # split on whitespace; wordList is now a list of words
    wordList = line.split()
    # Ignore empty lines
    if wordList == []:
        continue
    # Ignore territories
    if wordList[0] != "n/a":
        if len(wordList) == 11:
            state = wordList[2]
            population = wordList[3]
        else:
            state = wordList[2]+" "+wordList[3]
            population = wordList[4]
	# Store the population as the value, with stateName as the key
        population = population.replace(",","")
        pop = float(population)
        popDict[state] = [pop]


stateFile.close()
stateFile = open("StatePopulations.txt","r")

for line in stateFile:
    # split on whitespace; wordList is now a list of words
    wordList = line.split()
    # Ignore empty lines
    if wordList == []:
        continue
    # Ignore territories
    if wordList[0] != "n/a":
        if len(wordList) == 11:
            state = wordList[2]
            voteStr = wordList[5]
        else:
            state = wordList[2]+" "+wordList[3]
            voteStr = wordList[6]
	# Store the population as the value, with stateName as the key
        votes = float(voteStr)
        popDict[state] = popDict[state]+[votes]

stateFile.close()

for state in popDict:
    dataList = popDict[state]
    pop = dataList[0]
    votes = dataList[1]
    ratio = (votes/pop)*1000000
    print state,ratio






    
