Need use python to write a code. Question: Write a program that reads the rainfa
ID: 3781303 • Letter: N
Question
Need use python to write a code.
Question:
Write a program that reads the rainfall.txt then write out a new file called rainfallfmt.txt. The data
should be grouped on the normal annual rainfall field into the following categories: [51-60], [61-70],
[71-80], [81-90], [91-100]. Under each category, the new file should format each line so that the city
name is in upper case letters and is centered in a field that is 25 characters wide, and the rainfall
data should be printed in a field that is 5 characters wide with 1 digit to the right of the decimal
point.
I also need input data(.txt) into this program. Plz inculde how to open a file in the code.
Rainfall.text:
Akron 65.5574
Albia 95.631
Algona 77.9526
Allison 85.4456
Alton 69.6722
AmesW 86.5378
AmesSE 86.233
Anamosa 89.7382
Ankeny 84.7852
Atlantic 88.3158
Audubon 84.8614
Beaconsfield 89.5858
Bedford 92.329
BellePlaine 90.9574
Bellevue 87.249
Blockton 92.1512
Bloomfield 96.5708
Boone 92.202
Brighton 85.3186
Britt 80.1116
Buckeye 85.4964
BurlingtonKBUR 96.3676
Burlington 93.8276
Carroll 84.6582
Cascade 85.0392
Explanation / Answer
#This is the python code for given question, (python 2.7)
import operator
fr = open('Rainfall.txt','r') # open a file for reading the data
fw = open('rainfallfmt.txt','w') # open a file for writing the data
mymap = {}
for line in fr:
[city,rain] = line.strip().split(" ")
mymap[city] = float(rain)
fr.close()
sorted_mymap = sorted(mymap.items(), key=operator.itemgetter(1))
fw.write("CITYNAME")
fw.write(" ")
fw.write("RAINFALL")
fw.write(" ")
catagory = [60,70,80,90,100]
for c in catagory:
for e in sorted_mymap:
if((int(round(e[1],1)) > c - 10) and (int(round(e[1],1)) <= c) ):
fw.write(e[0].upper())
fw.write(" ")
fw.write(str(round(e[1],1)))
fw.write(" ")
fw.write(" ")
fw.close()
#Output file is as below (rainfallfmt.txt)
CITYNAME RAINFALL
AKRON 65.6
ALTON 69.7
ALGONA 78.0
BRITT 80.1
CARROLL 84.7
ANKENY 84.8
AUDUBON 84.9
CASCADE 85.0
BRIGHTON 85.3
ALLISON 85.4
BUCKEYE 85.5
AMESSE 86.2
AMESW 86.5
BELLEVUE 87.2
ATLANTIC 88.3
BEACONSFIELD 89.6
ANAMOSA 89.7
BELLEPLAINE 91.0
BLOCKTON 92.2
BOONE 92.2
BEDFORD 92.3
BURLINGTON 93.8
ALBIA 95.6
BURLINGTONKBUR 96.4
BLOOMFIELD 96.6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.