Engineers often analyze data given in a Comma Separated Valu

Engineers often analyze data given in a Comma Separated Values (CSV) file. A CSV file is a plain text file in which data are organized into rows and columns. A newline character 1n) makes a new line and a comma () separates adjacent columns. For example, the following file has five columns and three rows: 11.2,20.3,-36.4,9.3,5.5 22.5,18.6,-16.2,3.2,0.9 4.2,-16.2,13.2,-8.9,7.2 In this assignment, write a Python function to perform the following: Ask the user for the name of the CSV file to process Open the file for reading. Open another file with the name \"output.csv for writing Read the input file line by line For each line, compute the sum and the average of the column values. Write to the file \"output.csv\" the original column values followed by the row sum and the row average as a valid CSV file. In other words, the output file will have two more columns than the input file and the same number of rows Close both files A correct solution will generate the following data from the sample input file above and write the data into \"output.csv 11.2,20.3,-36.4,9.3,5.5,9.900000000000002,1.9800000000000004 22.5,18.6,-16.2,3.2,0.9,29.0,5.8 -34.2,-16.2,13.2,-8.9,7.,-38.9,-7.779999999999999 To verify the content of the \"output.csv file, you may want to open it using a spreadsheet application (e.g., LibreOffice Cale or Microsoft Excel) or a text editor,

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

 Engineers often analyze data given in a Comma Separated Values (CSV) file. A CSV file is a plain text file in which data are organized into rows and columns. A

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site