为什么它在CSV文件中创建了一个空行?

huangapple go评论64阅读模式
英文:

Why is it creating an empty row in the csv file?

问题

我尝试将数据写入CSV文件,但每次代码执行时都会创建一个空行。出了什么问题?

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)

数据输出如下,带有一个额外的空行:
为什么它在CSV文件中创建了一个空行?

如何移除它?提前感谢您的帮助 为什么它在CSV文件中创建了一个空行?

英文:

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:
为什么它在CSV文件中创建了一个空行?

How can I remove it? Thanks in advance for the help 为什么它在CSV文件中创建了一个空行?

答案1

得分: 1

根据我从你的代码中了解的情况,似乎你的CSV文件中有空行,这些空行被读入了data列表,然后你立即将它们写回文件中。

你已经确认你的原始CSV文件没有空格或空行吗?

英文:

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?

huangapple
  • 本文由 发表于 2023年8月4日 05:13:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定