Question

1. We have sever ral Fahrenheit degrees in a fi e Fdeg.dat and want to to read all of them into a list and convert the numbe

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code with indentation:

7 import csv 8 f open(. Fdeg.dat, r) #open file in read mode 9 Fdeg-[] 10 for line in f.readlines (): 12 ts]ine. split(:

Output:

Fdeg - Notepad File Edit Format View Help Fahr enheit degrees:67.2 Fahr enheit degrees:66.0 Fahr enheit degrees:78.9 Fahr enh

Result is stored in degrees.dat file

degrees-Notepad File Edit Format View Help 57.219. 56 66.0 18. 89 78.9 26. 06 102.138.94 32.0 0.0 87.831.0

While executing values of Fahrenheit and celsius in lists

Cdeg List (6 elements) Index Type Size e float 1 67.2 1 float 1 66.8 Index Type Size 0 float 1 19.56 1 float 18.89 2 float 1

____________________________________________________________________________________

Code:

import csv
f = open('Fdeg.dat', 'r') #open file in read mode
Fdeg=[]
for line in f.readlines():
t=[]
t=line.split(":") #splits line into two strings by delimiter :
Fdeg.append(float(t[1])) #appends temperature to list
f.close()

Cdeg=[] #list stores celcius temp

for i in range(len(Fdeg)):
cel=((5.0)/9)*((Fdeg[i])-32) #calculates celcius
Cdeg.append(round(cel,2)) #appends to list

zip(Fdeg,Cdeg) #zips two lists
with open('degrees.dat','w') as f: #open file in write mode
writer=csv.writer(f,delimiter='\t') #writes in file with delimiter space
writer.writerows(zip(Fdeg,Cdeg)) #write row by row in file
  
  

  
_____________________________________________________________________________________

Add a comment
Know the answer?
Add Answer to:
1. We have sever ral Fahrenheit degrees in a fi e 'Fdeg.dat and want to to read all of them into ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT