Python 中通过csv的writerow输出的内容有多余空行的解决办法
来源:互联网 发布:时间序列异常检测算法 编辑:程序博客网 时间:2024/05/29 04:35
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:
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的换行:
所以,要搞清楚,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,导致多余的空行。
- Python 中通过csv的writerow输出的内容有多余空行的解决办法
- Python中通过csv的writerow输出的内容有多余的空行
- Python中通过csv的writerow输出的内容有多余的空行
- Python中通过csv的writerow输出多余空行+py2.x &py3.兼容
- csv的writerow写入的文件会有空行很蛋疼
- csv.writer写入文件有多余的空行
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
- 控制台输出有空行的文件,输出时保留空行
- 用UEditor保存后出现多余空行问题的解决办法
- eclipse删除多余的空行
- php:去掉多余的空行
- DREAMWEAVER中消除代码中多余空行的方法
- python3 writerow CSV文件多一个空行
- python3 writerow csv文件多一个空行
- python3 writerow CSV文件多一个空行
- 去除word中多余的空格及空行
- 双二次,三次有理Bezier曲线表示第一象限1/8球面
- 回溯法算法思想解决最小机器重量设计问题
- FileZilla 向服务器传文件或下载文件
- 什么是DQL、DML、DDL、DCL
- mysql-installer-community-5.7.20.0.msi在Windows Server 2008上安装无反应
- Python 中通过csv的writerow输出的内容有多余空行的解决办法
- Dubbo服务暴露的流程
- python 字符串笔记整理12.17
- C#的GC机制,委托机制与Unity内存泄露
- 用 Ophiuchus 实现文字动画
- Kubernetes 1.9 发布
- 离线安装Tensorflow、Keras、OpenCV(任意库)
- 使用注解配置spring
- C语言小游戏---扫雷