第一种方法

如下生成的csv文件会有多个空行

import csv

#python2可以用file替代open
with open("test.csv","w") as csvfile:
writer = csv.writer(csvfile) #先写入columns_name
writer.writerow(["index","a_name","b_name"])
#写入多行用writerows
writer.writerows([[0,1,3],[1,2,3],[2,3,4]])

加入newline='' 参数

#coding=utf-8
import csv #python2可以用file替代open
with open("test.csv","wt",newline='') as csvfile:
writer = csv.writer(csvfile)
#写入多行用writerows
writer.writerows([["index","a_name","b_name"],[0,1,3],[1,2,3],[2,3,4]])

这样就不会有空行了。

PS:遇到问题没人解答?需要Python学习资料?可以加点击下方链接自行获取

note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76

第二种方法:

先写入csv文件,然后读出去掉空行,再写入

with open('E:\\test.csv','wt')as fout:       #生成csv文件,有空行
cout=csv.DictWriter(fout,list_attrs_head )
cout.writeheader()
cout.writerows(list_words)
with open('E:\\test.csv','rt')as fin: #读有空行的csv文件,舍弃空行
lines=''
for line in fin:
if line!='\n':
lines+=line
with open('E:\\test.csv','wt')as fout: #再次文本方式写入,不含空行
fout.write(lines)

最新文章

  1. Spring 4 异常处理
  2. UWP 设备分辨率
  3. HBase使用场景和成功案例 (转)
  4. 转:Nginx配置指令location匹配符优先级和安全问题
  5. ios开发——实用技术OC篇&地图与定位
  6. Yii2 自动更新时间created_at updated_at
  7. kindeditor使用方法
  8. AngularJS 跨站请求- jsonp请求
  9. Jboss 集群配置
  10. Java面试题—初级(3)
  11. linux 关机命令shutdown
  12. JS 作用域及作用域链
  13. mysql必须知道的
  14. 性能测试day05_Jmeter学习
  15. Vue Loader
  16. register form
  17. topcoder srm 713 div1
  18. Markdown 编辑器
  19. 在ASP.NET MVC中使用Knockout实践02,组合View Model成员、Select绑定、通过构造器创建View Model,扩展View Model方法
  20. openstack网络基础

热门文章

  1. fsockopen用feof读取http响应内容的一些问题
  2. 【Python成长之路】装逼的一行代码:快速共享文件【华为云分享】
  3. 从React 编程到"好莱坞"
  4. JSP + Session Cookie详解
  5. HDU5470 Typewriter (SAM+单调队列优化DP)
  6. ARTS-S centos修改hostname
  7. 基于iCamera测试高清摄像头SIV100B(替代ov7670)小结
  8. 深度优先搜索 & 广度优先搜索
  9. 搞清楚Spring Cloud架构原理的这4个点,轻松应对面试
  10. Spring与Shiro整合 加载权限表达式