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: 当上面的条件为假的的时候,才会执行. 执行顺序:先判断条件是否为真,如果是真的,执行循环体,再次判断条件,直到条件不成 ...
随机推荐
- WebService之Axis2 (3):使用services.xml文件发布WebService
用Axis2实现Web Service,虽然可以将POJO类放在axis2\WEB-INF\pojo目录中直接发布成Web Service,这样做不需要进行任何配置,但这些POJO类不能在任何包中.这 ...
- [LeetCode] 851. Loud and Rich_ Medium tag: DFS
In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...
- [LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...
- [LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- e.printStackTrace() ; 是什么意思?
catch(Exception e){e.printStackTrace() ;} 当try语句中出现异常是时,会执行catch中的语句,java运行时系统会自动将catch括号中的Exception ...
- linux命令:压缩解压命令
压缩解压命令:gzip 命令名称:gzip 命令英文原意:GNU zip 命令所在路径:/bin/gzip 执行权限:所有用户 语法:gzip 选项 [文件] 功能描述:压缩文件 压缩后文件格式:g ...
- Matlab中图像处理实例:灰度变换,空域滤波,频域滤波,傅里叶变换的实现
http://blog.sciencenet.cn/blog-95484-803140.html % %图像灰度变换 % f = imread('E:\2013第一学期课程\媒体计算\实验一\Img\ ...
- 安装vscode with springboot
1.安装jdk8 2.下载vscode,一切按照默认配置完成安装.下载地址:https://code.visualstudio.com 3.安装完成后,运行vscode.如果没有任何反应,在命令行上运 ...
- iOS获取本地ip和端口
#include <arpa/inet.h> #include <ifaddrs.h> #include <net/if.h> #define IOS_CELLUL ...
- MySQL笔记(五)MySQL 角色与SQL CHECK约束
MySQL ROLE MySQL 8.0 Reference Manual / Security / MySQL User Account Management / Using Roles ...