jeudi 30 avril 2015

Python read from file and show max and min total population

I'm working on this python code where I have to open up this file http://ift.tt/1An8fGj and read it so that it finds out the total population with the max and min from the years 1900 - 1990(Each state is on its own line, but the data are grouped so that only three decades of data are on each line complicating the task of extracting the data)I"m stuck I'd appreciate any help .. here is an example of how the finished code should look like

Enter census year 1900 to 1990: 1970
Min: (302853, 'Alaska')
Max: (19971069, 'Calfornia')

import sys

data = 0
##def total_population(pop_list):
##      check_year = int (input ("Enter a census year 1900 to 1990 (must end with 0) : "))
##      data = open ("urpop0090.txt","r")
##      temp_list = []
##
##      print ("check_year: ", (check_year))
##      #variables
##      max_pop = 0
##      min_pop = 1
##      tp = 0
##      temp_list = []
##      data = open ("urpop0090.txt","r")


def STATES(check_year):
      check_year.extend(input_year2)
      data = open("urpop0090.txt","r")
      check_year.remove("UNITED STATES")
      check_year.remove("%")
      check_year.pop("%")

def search(myFile, x, y):
    data = open("urpop0090.txt","r")

def check_year(year):
    if year >= 1900 and year <= 1990:
          if year % 10 == 0:
#X axis horizontally on x axis
        if year == 1990 or year == 1980 or year == 1970: #1st page
            x= 0
        elif year == 1960 or year == 1950 or year == 1940:#2nd page
            x = 52
        elif year == 1930 or year == 1920 or year == 1910:#3rd page
            x = 154
        elif year == 1900:
            x = 206
#Y axis going vertically by spaces
        if year == 1990 or year == 1960 or year == 1930 or year == 1900:#1st page
            y= 1
        elif year == 1980 or year == 1950 or year == 1920:#2nd page
            y = 7
        elif year == 1970 or year == 1940 or year == 1910:#3rd page
            y = 12
        #elif year == 1900:
              #y = 241

        return (x,y)



#population= total_population(data)
#print(population)
#Taking r's
data = open ("urpop0090.txt","r")
#main
line_num=0
year = int (input ("Enter a census year 1900 to 1990 (must end with 0) : "))
x,y = check_year(year)

print("x:", x, "y:", y)

temp_list = []
#Second column on x axis
for new_line in data :
    if new_line[0:5]=="     " and new_line[6]!=" ":
#Removing r letters
    r_removed = new_line.replace(" r ", "   ")
    comma_removed = r_removed.replace(",","")
    split_line = comma_removed.split()

#Converting column to an int
    try:       
        split_line[1] = int(split_line[1])
    except ValueError:
        try:
            split_line[2] = int(split_line[2])
            split_line[0] = split_line[0]+" "+split_line[1]
            split_line.pop(1)

        except ValueError:
                  print(ValueError)

##               for max in data:
##                     print data
##                     max = 0
##                     for i in range(1,len(words)):
##                           if len(words[i] > len(words[max]:
##                                                 max = i
##
        print(line_num)
        if line_num>= x and line_num<= x+ 50:
              print(line_num,split_line[0],split_line[y])

#developing a column of data (adding +1 until 50)
        line_num+=1
        if line_num == 50:
              break

    else:
        continue

year = 1990
coordinates = check_year(year)





# print(temp_list)

Aucun commentaire:

Enregistrer un commentaire