Thank you 58. Pogram: Work with Census Data rogram: Work With Census Data Use th
ID: 674670 • Letter: T
Question
Thank you
58. Pogram: Work with Census Data rogram: Work With Census Data Use this file: http://www.census.gov/population/www/censusdata/files/urpop0090.txt The census file is a text file with data for the 10-year census from 1900 to 1990 (e.g 1900, 1910, 1920, .. .). It has population data for each state as well as regional and overall data. 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. In addition, the data are further broken down into total, urban, rural, and percentages Write a program that for any census year input (e.g., 1970) the program will print the state and its total population with the minimum and maximum. For example: Enter census year 1900 to 1990: 1970 Minimum: (302853, 'Alaska') Maximum: (19971069, 'California') We had the output display a tuple as a hint to assist with solving the problem rather than illustrating readable output. Some points to consider: (a) Begin by generating clean data: there are footnotes that need to be eliminated, numbers contain commas, some rows (lines) have data you are not interested in (e.g., region data), you are not interested in all columns (e.g., percentages), and so on. Simply printing the lines with extraneous data removed is a good start. (b) You will likely want to combine multiple state name strings into one, e.g., "New" "York" becomes "New York."Explanation / Answer
censusvalues = {}
censusvalues['CA'] = {}
censusvalues['CA']['1960'] = <1960 census value>
censusvalues['CA'] = {}
censusvalues['CA']['1960'] = 456
censusvalues['CA']['1970'] = 789
>>censusvalues
>>{'CA': {'1960': 456, '1970': 789}}
state = raw_input("Enter the state: ")
year = raw_input("Enter the year: ")
censusvalues[name][year]
def main():
data = open('database', 'r')
lines = data.readlines()
year = 0
censusvalues = {}
for line in lines:
if re.match('<insert number of spaces here...too lazy to count>[0-9]', line):
year = int(line[<number of spaces>:].strip())
continue
if line.startswith(' '):
firsttotalpop = getsub(line, 42)
secondtotalpop = getsub(line, 100)
thirdtotalpop = getsub(line, 158)
censusvalues[state][year] = firsttoalpop
censusvalues[state][year-10] = secondtotalpop
censusvalues[state][year-20] = thirdtotalpop
if not <state> in censusvalues:
censusvalues[<state>] = {}
return 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.