PLEASE USE PYTHON 3.4 The data file for this program is named CityPopulation.txt
ID: 3667947 • Letter: P
Question
PLEASE USE PYTHON 3.4
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?
This should be in a file called CityPopulation.txt
Explanation / Answer
This is not a complete solution... if i could get some more time... Then i could complete it
from collections import defaultdict
fo=open("citypopulation","r")
#here we have used collections in python
new_file = open('output_file.txt', 'w')
new_f = open('output_f.txt', 'w')
d = defaultdict(list)
count=0
for line in fo:
s = line.split(',')
if d.has_key(s[0]):
new_file.write(s[0]+' '+s[3])
d[s[0]]+=' '.join(s[3])
else:
new_file.write(s[0]+' '+s[3])
d[s[0]]=' '.join((s[3]))
count=count+int(s[3])
for key in d:
new_f.write("%s %s "% (key,d[key]))
for key in d:
print "%s %s "% (key,d[key])
print "Total amount is"
print count
fo.close()
new_file.close()
new_f.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.