0

I'm trying to write data to a csv file but it's creating an empty row each time the code executes. What is going wrong there?

  data = []
with open ("employee_data.csv","r") as employee_data:
                reader = csv.reader(employee_data)
                user_id = "336456"
                
                for row in reader:
                     if row[0] == user_id:
                       
                         
                        leave_type = "n"

                        row.append(leave_type)
                     data.append(row)
                        
with open ("employee_data.csv","w") as employee_data:
    writer = csv.writer(employee_data)
    writer.writerows(data)

The data comes out like this with a space which is an extra line: enter image description here

How can I remove it? Thanks in advance for the help :)

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
amsi_25
  • 23
  • 4

1 Answers1

1

From what I can gather from your code, it looks like your csv file has empty rows that are being read into the data list, which you are then promptly writing back into the file.

Have you verified that your original csv file doesn't have blank spaces/rows?

  • The csv initially had no data except from the first line: 33645, A,S. I ran the code to insert the letter n into that row. However, when I do that it also inserts a blank line underneath that row and this is something that I want to prevent. How can I do that? – amsi_25 Aug 03 '23 at 22:03