Avoiding empty line in writing process csv file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Avoiding empty line in writing process csv file (/thread-18244.html) |
Avoiding empty line in writing process csv file - go127a - May-10-2019 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: 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 RE: Avoiding empty line in writing process csv file - perfringo - May-10-2019 Maybe you could ask yourself a question - if you write-append to an existing csv file, where do you want your first line to be written? On the same line as last line in file or on a new line after last line? RE: Avoiding empty line in writing process csv file - go127a - May-10-2019 its correct there is no problem with writing, the csv file should be rewrite by the results, but the problem is, I have two line in the output (one extra line, which is unnecessary) the problem produced by 'writerows' i know that, but I can not find, how can I write the output except 'writerows' csv writing process. https://docs.python.org/3/library/csv.html RE: Avoiding empty line in writing process csv file - perfringo - May-10-2019 Unfortunately you didn't ask the question I suggested. How could (any) program know when and where row ends? There is newline at the end of every row. If you write a row it will end with newline. What will happen if there is newline at the end of last row? RE: Avoiding empty line in writing process csv file - go127a - May-10-2019 I have used below code for writing, is it correct? (i mean is there any easier way?) for item in data: dataf=str(item) with open('grades.csv', 'w', newline='') as output_file_name: output_file_name.write(dataf) output_file_name.close() |