最新消息:服务器现已从linode换到fzhost.net,且主题已从retina 0.2换为大前端D8

【已解决】Python中通过csv的writerow输出的内容有多余的空行

Python crifan 27浏览 0评论

【问题】

Python中,通过csv的writerow输出内容:

    #output all info dict list
    outputFp = open(gConst['csvFilename'], 'a+');
    csvWriter = csv.writer(outputFp, dialect='excel');
    for eachInfoDict in itemInfoDictList:
        fieldList = [];
        fieldList.append(eachInfoDict['Lead Source']);
...
        logging.info("fieldList=%s", fieldList);

        csvWriter.writerow(fieldList);
    outputFp.close();

结果却发现输出了csv中,每一行row之后,有个多余的空行:

csv redundant row

用excel打开后,效果如下:

redundant new line

现在需要去掉这个多余的空行。

【解决过程】

1.去查了查writerow:

http://docs.python.org/2/library/csv.html#writer-objects

13.1.4. Writer Objects

Writer objects (DictWriter instances and objects returned by the writer() function) have the following public methods. A row must be a sequence of strings or numbers for Writer objects and a dictionary mapping fieldnames to strings or numbers (by passing them through str() first) for DictWriter objects. Note that complex numbers are written out surrounded by parens. This may cause some problems for other programs which read CSV files (assuming they support complex numbers at all).

csvwriter.writerow(row)

Write the row parameter to the writer’s file object, formatted according to the current dialect.

csvwriter.writerows(rows)

Write all the rows parameters (a list of row objects as described above) to the writer’s file object, formatted according to the current dialect.

 

但是貌似没太大帮助。

2.后来注意到,输出的csv的效果是:

行末是CR

然后才是一个CRLF的换行:

end of line is CR then CR LF

所以,要搞清楚,CR和CRLF 分别是谁输出的。

3.后来参考:

Extraneous newlines with csv.writer on Windows

说是,使用binary模式即可。

所以把:

    #init output file
    # 'a+': read,write,append
    # 'w' : clear before, then write
    outputFp = open(gConst['csvFilename'], 'w');
    csvWriter = csv.writer(outputFp, dialect='excel');
    # itemInfoDict = {
...    csvWriter.writerow(csvHeaderList);
    outputFp.close();

    #output all info dict list
    outputFp = open(gConst['csvFilename'], 'a+');
    csvWriter = csv.writer(outputFp, dialect='excel');
    for eachInfoDict in itemInfoDictList:
        fieldList = [];
        fieldList.append(eachInfoDict['Lead Source']);
...    outputFp.close();

去改为:

    #init output file
    # 'a+': read,write,append
    # 'w' : clear before, then write
    outputFp = open(gConst['csvFilename'], 'wb');
    csvWriter = csv.writer(outputFp, dialect='excel');
...    csvWriter.writerow(csvHeaderList);
    outputFp.close();

    #output all info dict list
    outputFp = open(gConst['csvFilename'], 'a+');
    csvWriter = csv.writer(outputFp, dialect='excel');
    for eachInfoDict in itemInfoDictList:
        fieldList = [];
...        logging.info("fieldList=%s", fieldList);

        csvWriter.writerow(fieldList);
    outputFp.close();

试试,结果是,

标题那一行,由于是二进制的wb打开的文件,所以OK了,没有多余的CR了

但是其余的各行,由于是文本方式的a+打开的,结果还是有多余的CR:

header is ok but other line is still has CRLF

4.所以再去改为:

    outputFp = open(gConst['csvFilename'], 'wb');
    csvWriter = csv.writer(outputFp, dialect='excel');
    ...
    csvWriter.writerow(csvHeaderList);
    outputFp.close();
    
    ...
    ...
    
    #output all info dict list
    #outputFp = open(gConst['csvFilename'], 'a+');
    outputFp = open(gConst['csvFilename'], 'ab+');
    csvWriter = csv.writer(outputFp, dialect='excel');
    ......
    csvWriter.writerow(fieldList);
    outputFp.close();

试试效果,结果终于可以了:

CSV中,没有了多余的CR了,只有行尾的CRLF:

csv only last has the CRLF

对应的excel中,也可以显示正常,没有多余的空行了:

excel can show ok no extra empy line

 

【总结】

Python中的csv的writer,打开文件的时候,要小心,

要通过binary模式去打开,即带b的,比如wb,ab+等

而不能通过文本模式,即不带b的方式,w,w+,a+等,否则,会导致使用writerow写内容到csv中时,产生对于的CR,导致多余的空行。

 

注:关于文件打开的方式,是binary还是text,详见:

【详解】Python中的文件操作,readline读取单行,readlines读取全部行,文件打开模式

转载请注明:在路上 » 【已解决】Python中通过csv的writerow输出的内容有多余的空行

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (6)

  1. 用newline=”解决了
    Ancient10个月前 (08-23)回复
  2. 简直就是扯蛋啊。。。怎么可能把str类型当成byte类型写入文件里。。醉了,还说得有模有样。。
    Ancient10个月前 (08-23)回复
  3. 大哥,我觉得你能写这么多帮助文档真的很不错... 但是读起来真的很费力...能不能把废话放在后面,先讲重点...
    laplaceyin3年前 (2014-10-28)回复
    • 而且...我弄成二进制文件以后不能写入中文了...
      laplaceyin3年前 (2014-10-28)回复
      • http://stackoverflow.com/questions/14693646/writing-to-csv-file-python?lq=1 作者的文章给了提示,在新版3.x的版本中,如果用二进制写的话,有些问题,比如与正则表达式的结果就无法使用二进制方式写入,所以上述连接可以找到答案。 即: with open('test.csv', 'w', newline='') as fp: 加一个"newline"参数即可。
        xx3年前 (2014-11-13)回复
    • 详见: 关于本站帖子的一些解释 中的“为何不直接把结论写在帖子最开始?”
      crifan3年前 (2014-10-29)回复
64 queries in 0.547 seconds, using 12.56MB memory