Python csv writer是否始终使用DOS行尾字符? [英] Does Python csv writer always use DOS end-of-line characters?

查看:106
本文介绍了Python csv writer是否始终使用DOS行尾字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到Python中的 csv 库始终会生成DOS行尾字符.即使我使用'wb'模式,即使我使用Linux.

I realize that the csv library in Python always generates DOS end-of-line characters. Even if I use the 'wb' mode, even if I use Linux.

import csv

f = open('output.txt', 'wb');
writer = csv.writer(f)
writer.writerow([2,3,4]);
f.close()

上面的代码始终将'\r\n'用作行分隔符的结尾.我怎样才能仅使用'\n'来使用它?

The above code always uses '\r\n' as the end of line separator. How can I make it use use '\n' only?

推荐答案

您可以为您的writer 实例自定义 lineterminator 构造函数中的参数:

You can give your writer instance a custom lineterminator argument in the constructor:

writer = csv.writer(f, lineterminator="\n")

这篇关于Python csv writer是否始终使用DOS行尾字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆