Python写入CSV文件的问题
这篇文章主要是前几天我处理数据时遇到的三个问题:
- Python写入的csv的问题
- Python2与Python3处理写入写入空行不同的处理方式
- Python与Python3的编码问题
其实上面第3个问题是一个大问题,本文暂且不表,主要说明前两个问题。
第一个问题###
先看一下官方文档给出的例子:
headers = ['Symbol','Price','Date','Time','Change','Volume']
rows = [('AA', 39.48, '6/11/2007', '9:36am', -0.18, 181800),
('AIG', 71.38, '6/11/2007', '9:36am', -0.15, 195500),
('AXP', 62.58, '6/11/2007', '9:36am', -0.46, 935000),
]
with open('stocks.csv','w') as f:
f_csv = csv.writer(f)
f_csv.writerow(headers)
f_csv.writerows(rows)
但是上述代码并不能得到我们想要的格式,它是横着排的,其实excel只有一行数据。
无奈,我不知道问题出在哪里,我只能一行行的写入。
for i in range(0, len(list)):
f_csv.writerow(list[i])
第二个问题###
关于写入excel时,文档多出空行的问题,python2和3有不同的处理。
先看Python2,以二进制wb的方式写入即可。
writefile = open('result.csv','wb')
writer = csv.writer(writefile)
Python3使用上述方式会报错:TypeError: 'str' does not support the buffer interface
解决方法如下:以w模式打开文件,添加参数newline=''
outputfile=open("out.csv",'w',encoding='utf8',newline='')
**
In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling.
In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is:
outputfile=open("out.csv",'w',encoding='utf8',newline='')
encoding can be whatever you require, but newline='' suppresses text mode newline handling. On Windows, failing to do this will write \r\r\n file line endings instead of the correct \r\n. This is mentioned in the 3.X [csv.reader][2] documentation only, but [csv.writer][3] requires it as well.
**
参考###
- http://blog.csdn.net/pfm685757/article/details/47806469
- http://www.cnblogs.com/meitian/p/4625011.html
- http://aleeee.com/python23_str.html
- http://blog.csdn.net/xiaobing_blog/article/details/14056473
Python写入CSV文件的问题的更多相关文章
- 利用Python写入CSV文件的方法
第一种:CSV写入中文 #! /usr/bin/env python # _*_ coding:utf- _*_ import csv csvfile = file('test.csv', 'wb') ...
- python写入csv文件时的乱码问题
今天在使用python的csv库将数据写入csv文件时候,出现了中文乱码问题,解决方法是在写入文件前,先指定utf-8编码,如下: import csv import codecs if __name ...
- python写入csv文件的几种方法总结
生成test.csv文件 #coding=utf- import pandas as pd #任意的多组列表 a = [,,] b = [,,] #字典中的key值即为csv中列名 dataframe ...
- python 写入csv文件
import csv fieldnames = ['Column1', 'Column2', 'Column3', 'Column4'] rows = [{'Column1': '0', 'Col ...
- Python写入csv文件示例
import csv header = ['City', 'AQI', 'PM2.5/1h', 'PM10/1h', 'CO/1h', 'NO2/1h', 'O3/1h', 'O3/8h', 'SO2 ...
- python读取和写入csv文件
读取csv文件: def readCsv(): rows=[] with file(r'E:\py\py01\Data\system.csv','rb') as f: reads=csv.reader ...
- 解决python中csv文件中文写入问题
一.前言 一般来说,为了方便,使用python的时候都会使用csv模块去写数据到csv文件,但是写入中文的时候,经常会报错: UnicodeEncodeError: 'ascii' codec can ...
- python在不同情况下写入csv文件
情况一(解法一):将列表存储为csv文件.列表的每一项代表csv文件的一行. 列表中的每一项包含多个属性.list=[[属性1,属性2,属性3,……],[属性1,属性2,属性3,……],[属性1,属性 ...
- python之读取和写入csv文件
写入csv文件源码: #输出数据写入CSV文件 import csv data = [ ("Mike", "male", 24), ("Lee&quo ...
随机推荐
- python全栈开发从入门到放弃之装饰器函数
什么是装饰器#1 开放封闭原则:对扩展是开放的,对修改是封闭的#2 装饰器本身可以是任意可调用对象,被装饰的对象也可以是任意可调用对象#3 目的:''' 在遵循 1. 不修改被装饰对象的源代码 2. ...
- yii2 商品上下架
视图层 <td><?php if($value['is_on_sale'] == 1) {?><img src="../web/images/yes.gif&q ...
- vuex是什么?怎么用,例子
什么是vuex? 官方的解释是:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 为什么要用 ...
- 乐观锁的一种实现方式——CAS
在java里面,synchronized关键字就是一种悲观锁,因为在加上锁之后,只有当前线程可以操作变量,其他线程只有等待. CAS操作是一种乐观锁,它假设数据不会产生冲突,而是在提交的时候再进行版本 ...
- 安全SOCKET
导语 要使用安全Socket需要对密码学有一定的了解.在阅读本文之前最好能阅读一下下面几个网站的内容 http://kb.cnblogs.com/page/194742/ http://kb.cnbl ...
- java: -source 1.6 中不支持 switch 中存在字符串
最近在使用IDEA进行单个文件编译的时候给我报错,如题. 解决办法:将 Modules --->Sources ---> Language level 改为 7.0就ok了.
- php下获取http状态的实现代码
在项目开发中,有时我们需要知道远程的URL地址是否能访问正常,判断其正常与否后进行下一步的操作,那么在PHP中如何获取远程HTTP的状态呢? 文件preg.php header("HTTP/ ...
- center os7.2 apache+php+mysql环境配置并设置https访问
本人阿里云购买的center os7.2系统,小程序只支持https,因此需要配置https 安装apache yum -y install httpd systemctl start httpd a ...
- Painter's Problem
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5378 Accepted: 2601 Description There ...
- Linux kernel 编译问题记录【转】
本文转载自:http://sunyongfeng.com/201701/programmer/linux/kernel_compile_fail.html 编译内核出现以下错误 Can't use ' ...