Python Forum
Avoiding empty line in writing process csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Avoiding empty line in writing process csv file
#1
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
(this line is empty but exist)
Reply
#2
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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
Reply
#4
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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create csv file with 4 columns for process mining thomaskissas33 3 741 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,527 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 856 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,360 Sep-27-2022, 01:38 PM
Last Post: buran
  Failed to execute child process (No such file or directory) uriel 1 1,648 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Writing to json file ebolisa 1 993 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,478 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Print to a New Line when Appending File DaveG 0 1,216 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,446 Mar-18-2022, 07:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020