May-10-2019, 12:06 PM
Hallo, I have done some calculation on csv file. However, the output csv, after writing process added one empty line at the end of results.
My question is how can I remove or avoid that line.
the code is here:
but now I got the csv file like:
My question is how can I remove or avoid that line.
the code is here:
import csv
from statistics import mean
import itertools
from collections import OrderedDict
def calculate_average_of_averages(input_file_name, output_file_name):
with open ('grades.csv', 'r') as input_file_name:
reader=csv.reader(input_file_name)
val1=[]
key=list()
data=[]
for row in reader:
l2 = []
k = row[0]
grades=[int(num) for num in row[1:]]
l2.append(float(mean(grades)))
key.append(k) #making a name -key- list
val1.append(l2) #making a value list
value = list(itertools.chain.from_iterable(val1)) #making a simple list from list of list in value
value=[float(i) for i in value] ##changing string to float in values
dictionary = dict(zip(key, value))
findic=OrderedDict(sorted(dictionary.items(), key=lambda t: t[1])) ##making a sorted list by OrderedDict
lv=[]
for item in findic.values():
lv.append(item)
data.append(float(mean(value)))
with open('grades.csv', 'w', newline='') as output_file_name:
writer = csv.writer(output_file_name)
writer.writerows(map(lambda x: [x], data))
output_file_name.close()
calculate_average_of_averages('input.csv','output.csv')the correct output is:Quote:8.401530612244898
but now I got the csv file like:
Quote:8.401530612244898
(this line is empty but exist)
