Engineers often analyze data given in a Comma Separated Valu
Solution
def csv():
print(\"CSV Data analysis\")
print()
fname = str(input(\"Enter CSV file name: \")) #inputing file name
output_file = \"output.csv\" #defining output file
#Reading the input file into a list with each row as a string
with open(fname) as f:
input_data = f.read().splitlines()
#initialising data
data = []
#iterating through the list containing input data and storing each row as a list of floats.
for row in input_data:
row_data = [float(x) for x in row.split(\',\')]
data.append(row_data)
#initialising output
output = []
#iterating through the data to be worked on
for row in data:
#initialising variables for sum, number of elements in a row and row data that needs to be output.
s = 0
n = 0
row_data = []
#iterating through the elements of the row
for elem in row:
s += elem #adding to the sum
n += 1.0 #incrementing the number of elements
row_data.append(elem) #adding the element to the final output row
row_data.append(s) #adding the sum to the row
row_data.append(s/n) #adding the average to the row
output.append(row_data) #adding each row to the final list of lists.
#outputing to a csv file
f = open(output_file, \"w\")
for row in output:
for elem in row:
f.write(str(elem))
f.write(\',\')
f.write(\"\ \")
print(\"Job done.\")
csv() #calling the function
