Python中通过csv的writerow输出的内容有多余的空行
第一种方法
如下生成的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([[,,],[,,],[,,]])
加入newline='' 参数
#coding=utf-
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"],[,,],[,,],[,,]])
这样就不会有空行了。
第二种方法:
先写入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)
参考:
https://www.crifan.com/python_csv_writer_writerow_redundant_new_line/?utm_source=tuicool&utm_medium=referral
https://blog.csdn.net/langaer/article/details/70052971
Python中通过csv的writerow输出的内容有多余的空行的更多相关文章
- Python中通过csv的writerow输出的内容有多余的空行两种方法
第一种方法 如下生成的csv文件会有多个空行 import csv #python2可以用file替代open with open("test.csv","w" ...
- python中操作csv文件
python中操作csv文件 读取csv improt csv f = csv.reader(open("文件路径","r")) for i in f: pri ...
- Python中日期和时间格式化输出的方法
本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...
- 2. Python中的基本输入、输出、格式化
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的基本输入.输出.格式化 1. 输入 使用input([prompt])读取一行,将其转换为string类型并返回,input的 ...
- Python中关于csv的简单操作
Python中关于csv的简单操作 CSV操作简单,直接import csv即可, 主要使用reader和pandas 1 reader的简单使用 csv.reader("1.csv&quo ...
- Python中的输入(input)和输出打印
目录 最简单的打印 打印数字 打印字符 字符串的格式化输出 python中让输出不换行 以下的都是在Python3.X环境下的 使用 input 函数接收用户的输入,返回的是 str 字符串 最简单的 ...
- (转)Ubuntu中让终端对于历史输出的内容保持足够长
原地址:http://www.crifan.com/ubuntu_terminal_make_retain_long_enough_history_output_content/ Ubuntu下用终端 ...
- Python中读取csv文件内容方法
gg 224@126.com 85 男 dd 123@126.com 52 女 fgf 125@126.com 23 女 csv文件内容如上图,首先导入csv包,调用csv中的方法reader()创建 ...
- python中的while循环,格式化输出,运算符,编码
一.while循环 1.1语法 while 条件: 代码块(循环体) else: 当上面的条件为假的的时候,才会执行. 执行顺序:先判断条件是否为真,如果是真的,执行循环体,再次判断条件,直到条件不成 ...
随机推荐
- [py]python多态-动态语言的鸭子类型
弱类型?强类型?动态语言,静态语言 弱类型: 在程序运行过程中,类型可变 还有一种说法: 动态 variables must necessarily be defined before they ar ...
- POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)
http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...
- Tomcat修改端口和编码配置
1.打开Tomcat中server.xml文件,找到原本的8080,修改成没被占用的端口: 2.在这个标签里增加 URIEncoding="utf-8",修改请求的编码.
- [Leetcode] 336. Palindrome Pairs_Hard
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...
- pop to 特定的UIViewController
1. 我们可以推出到特定的UIViewController 2. 有一个类没有navigationController,以前一般用delegate,我觉得我们可以把引用一个navigationCont ...
- 一个新人对HTML的理解
首先 HTML里面包含的东西是什么? 在HTML里面 注释的表示方式是 <!--注释内容--> 注释 HTML初始默认包含了两大部分: 一部分是 <head>< ...
- 【调优】Nginx性能调优
一.Nginx优化配置 1.主配置文件优化:# vi /usr/local/nginx/conf/nginx.conf----------------------------------------- ...
- C#中NPOI操作excel之读取和写入excel数据
一.下载引用 下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2 ...
- easyDialog参数配置说明
easyDialog不依赖框架,使用起来很简单,只要引入easydialog.js文件就可以使用了: // 引入easyDialog <script src="easydialog.j ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON HomMat2dRotate2
zw版[转发·台湾nvp系列Delphi例程]HALCON HomMat2dRotate2 procedure TForm1.Button1Click(Sender: TObject);var op ...