删除csv中的最后一行 [英] Removing last row in csv
本文介绍了删除csv中的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试删除csv中的最后一行,但出现错误: _csv.Error:字符串为NUL字节
I'm trying to remove the last row in a csv but I getting an error: _csv.Error: string with NUL byte
这是我到目前为止所拥有的:
This is what I have so far:
dcsv = open('PnL.csv' , 'a+r+b')
cWriter = csv.writer(dcsv, delimiter=' ')
cReader = csv.reader(dcsv)
for row in cReader:
cWriter.writerow(row[:-1])
我不知道为什么我总是出错
I cant figure out why I keep getting errors
推荐答案
我只需要使用readlines()读取整个文件,弹出最后一行,然后使用csv模块将其写入
I would just read in the whole file with readlines(), pop out the last row, and then write that with csv module
import csv
f = open("summary.csv", "r+w")
lines=f.readlines()
lines=lines[:-1]
cWriter = csv.writer(f, delimiter=',')
for line in lines:
cWriter.writerow(line)
这篇关于删除csv中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文