Python CSV Writer 在文件末尾留空行
Python CSV Writer leave a empty line at the end of the file
以下代码在 txt 文件末尾留下一个空的白行。我怎么能让 writerows 不终止最后一行?
with open(fname, 'wb') as myFile:
# Start the CSV Writer
wr = csv.writer(myFile, delimiter=',', dialect='excel')
wr.writerows(rows)
# Close the file.
myFile.close()
首先,由于您使用的是 with open as myFile
,因此不需要 myFile.close()
,当您删除缩进时,它会自动完成。
其次,如果您愿意在您的程序中添加另一部分,您可以简单地编写一些删除最后一行的内容。 Strawberry 做了一个例子(稍作修改):
with open(fname) as myFile:
lines = myFile.readlines()
with open(fname, "w") as myFile:
myFile.writelines([item for item in lines[:-1]])
注意'w'参数是如何清除文件的,所以我们需要打开文件两次,一次读取,一次写入。
我也相信,您可以使用不添加换行符的myFile.write
。使用它的一个例子是:
with open(fname, 'wb') as myFile:
wr = csv.writer(myFile, delimiter=',', dialect='excel')
lines = []
for items in rows:
lines.append(','.join(items))
wr.write('\n'.join(lines))
然而,这仅在您拥有多维数组时才有效,应该避免。
我找不到适用于 python 3 的答案,也适用于我的情况,所以这是我的解决方案:
def remove_last_line_from_csv(filename):
with open(filename) as myFile:
lines = myFile.readlines()
last_line = lines[len(lines)-1]
lines[len(lines)-1] = last_line.rstrip()
with open(filename, 'w') as myFile:
myFile.writelines(lines)
我很感激这是一个旧请求,但我在寻找相同的解决方案时偶然发现了它。我最终阅读了 the csv documentation itself 中的答案,发现 csv.writer 有一个 lineterminator
格式化参数,默认为 \r\n
,给出了我们都不想要的新行。
作为解决方案,我在代码中添加了格式化参数 newline=''
,它运行良好(在下面的原位)。
with open(fname, 'wb') as myFile:
# Start the CSV Writer
wr = csv.writer(myFile, delimiter=',', dialect='excel', lineterminator='')
wr.writerows(rows)
感谢 wfgeo
其中一个解决方案是这样工作的。虽然它需要 os
库,但它让生活变得更轻松:
import csv
import os
with open(fileName, 'w', newline='') as f:
writer = csv.writer(f, delimiter=';', dialect='excel')
for row in rows:
row = rows[0].values()
writer.writerow(row)
f.seek(0, os.SEEK_END)
f.seek(f.tell()-2, os.SEEK_SET)
f.truncate()
以下代码在 txt 文件末尾留下一个空的白行。我怎么能让 writerows 不终止最后一行?
with open(fname, 'wb') as myFile:
# Start the CSV Writer
wr = csv.writer(myFile, delimiter=',', dialect='excel')
wr.writerows(rows)
# Close the file.
myFile.close()
首先,由于您使用的是 with open as myFile
,因此不需要 myFile.close()
,当您删除缩进时,它会自动完成。
其次,如果您愿意在您的程序中添加另一部分,您可以简单地编写一些删除最后一行的内容。 Strawberry 做了一个例子(稍作修改):
with open(fname) as myFile:
lines = myFile.readlines()
with open(fname, "w") as myFile:
myFile.writelines([item for item in lines[:-1]])
注意'w'参数是如何清除文件的,所以我们需要打开文件两次,一次读取,一次写入。
我也相信,您可以使用不添加换行符的myFile.write
。使用它的一个例子是:
with open(fname, 'wb') as myFile:
wr = csv.writer(myFile, delimiter=',', dialect='excel')
lines = []
for items in rows:
lines.append(','.join(items))
wr.write('\n'.join(lines))
然而,这仅在您拥有多维数组时才有效,应该避免。
我找不到适用于 python 3 的答案,也适用于我的情况,所以这是我的解决方案:
def remove_last_line_from_csv(filename):
with open(filename) as myFile:
lines = myFile.readlines()
last_line = lines[len(lines)-1]
lines[len(lines)-1] = last_line.rstrip()
with open(filename, 'w') as myFile:
myFile.writelines(lines)
我很感激这是一个旧请求,但我在寻找相同的解决方案时偶然发现了它。我最终阅读了 the csv documentation itself 中的答案,发现 csv.writer 有一个 lineterminator
格式化参数,默认为 \r\n
,给出了我们都不想要的新行。
作为解决方案,我在代码中添加了格式化参数 newline=''
,它运行良好(在下面的原位)。
with open(fname, 'wb') as myFile:
# Start the CSV Writer
wr = csv.writer(myFile, delimiter=',', dialect='excel', lineterminator='')
wr.writerows(rows)
感谢 wfgeo
其中一个解决方案是这样工作的。虽然它需要 os
库,但它让生活变得更轻松:
import csv
import os
with open(fileName, 'w', newline='') as f:
writer = csv.writer(f, delimiter=';', dialect='excel')
for row in rows:
row = rows[0].values()
writer.writerow(row)
f.seek(0, os.SEEK_END)
f.seek(f.tell()-2, os.SEEK_SET)
f.truncate()