By using PYthon:The data file for this program is named CityPopulation.txt. Usin
ID: 3667665 • Letter: B
Question
By using PYthon:The data file for this program is named CityPopulation.txt. Using the data in the file, produce a report that provides a list of the counties and specifies each county’s total urban population. For example, the entries for Yellow Medicine County are: Yellow Medicine,Canby,1795,1742 Yellow Medicine,Clarkfield,863,831 Yellow Medicine,Echo,278,270 Yellow Medicine,Granite Falls,2897,2796 Yellow Medicine,Hanley Falls,304,295 Yellow Medicine,Hazel Run,63,61 Yellow Medicine,Porter,183,183 Yellow Medicine,St. Leo,100,97 Yellow Medicine,Wood Lake,439,417 The first numeric value was the actual population of each city in the 2010 census. The second value is the estimated population for 2014. Use the 2014 data. Do not change the original data file. Each line of the report will have the results for one county. At the very end of the report, give the total urban population for the entire state. Yellow Medicine: 6,692 The actual files that I will use for testing will have the same name and the same format as CityPopulation.txt, so that name should be hard-coded into your program. The file will *not* be the same size as the original. (I will use a smaller set of about five counties.) Your output should be written into a file named Prog3.txt, **but** preceded by your initials. For example, if your name was Donald Trump, your output file would be DTPRog3.txt. Your output should **also** be displayed on the screen. When you think you are done, double-check: CityPopulation.txt is hard-coded as the name of the input file? Output into XXProg3.txt? Output also displayed on the screen? Use only the features of Python found in Chapters 1-4 and 7 of the text. Please use the python programming language!!
Explanation / Answer
input = open('CityPopulation.txt','r')
county_dict = {}
for line in input:
line = line.rstrip().split(',')
if line[0] in county_dict:
county_dict[line[0]]+=int(line[-1])
else:
county_dict[line[0]]=int(line[-1])
output =open('DTPRog3.txt','w')
total_count = 0
for item in county_dict:
output.write(item+': '+str(county_dict[item])+' ')
print item,": ", county_dict[item]
total_count+= county_dict[item]
output.write(str(total_count))
print "total_population: ",total_count
output.close()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.