Answer:
In Python:
file = input("File name: ")
f=open(file,"r")
lines=f.readlines()
col1=[]
col2=[]
for x in lines:
col1.append(float(x.split(' ')[0]))
for x in lines:
col2.append(float(x.split(' ')[1].rstrip('\\')))
f.close()
sum1 = 0.0
sum2 = 0.0
for i in range(len(col1)):
sum1+=col1[i]
sum2+=col2[i]
print("Average of Column 1: %.2f" %(sum1/len(col1)))
print("Average of Column 2: %.2f" %(sum2/len(col2)))
Step-by-step explanation:
This prompts the user for file name
file = input("File name: ")
This opens the file
f=open(file,"r")
This reads the lines of the file
lines=f.readlines()
This creates an empty list for column 1
col1=[]
This creates an empty list for column 2
col2=[]
The following iterations iterate through the lines and puts them in the respective column list
for x in lines:
col1.append(float(x.split(' ')[0]))
for x in lines:
col2.append(float(x.split(' ')[1].rstrip('\\')))
This closes the file
f.close()
This initializes the sum of column 1 to 0
sum1 = 0.0
This initializes the sum of column 2 to 0
sum2 = 0.0
This iterates through each list and adds up the list contents
for i in range(len(col1)):
sum1+=col1[i]
sum2+=col2[i]
This calculates prints the average of the first column to 2 d.p
print("Average of Column 1: %.2f" %(sum1/len(col1)))
This calculates prints the average of the second column to 2 d.p
print("Average of Column 2: %.2f" %(sum2/len(col2)))