兜率天童,扑倒老公大人迪文,智利女星16小时
第一种方法
如下生成的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)
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论