當前位置:成語大全網 - 新華字典 - Python中通過csv的writerow輸出的內容有多余的空行

Python中通過csv的writerow輸出的內容有多余的空行

問題

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之後,有個多余的空行:

用excel打開後,效果如下:

現在需要去掉這個多余的空行。

解決過程

1.去查了查writerow:

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的換行:

所以,要搞清楚,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:

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:

對應的excel中,也可以顯示正常,沒有多余的空行了:

總結

Python中的csv的writer,打開文件的時候,要小心,

要通過binary模式去打開,即帶b的,比如wb,ab+等

而不能通過文本模式,即不帶b的方式,w,w+,a+等,否則,會導致使用writerow寫內容到csv中時,產生對於的CR,導致多余的空行。

註:關於文件打開的方式,是binary還是text,詳見:

詳解Python中的文件操作,readline讀取單行,readlines讀取全部行,文件打開模式

閱讀全文