英文:
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)
如何移除它?提前感谢您的帮助
英文:
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:
How can I remove it? Thanks in advance for the help
答案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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论