这篇文章主要是前几天我处理数据时遇到的三个问题:

  1. Python写入的csv的问题
  2. Python2与Python3处理写入写入空行不同的处理方式
  3. 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.

**


参考###

Python写入CSV文件的问题的更多相关文章

  1. 利用Python写入CSV文件的方法

    第一种:CSV写入中文 #! /usr/bin/env python # _*_ coding:utf- _*_ import csv csvfile = file('test.csv', 'wb') ...

  2. python写入csv文件时的乱码问题

    今天在使用python的csv库将数据写入csv文件时候,出现了中文乱码问题,解决方法是在写入文件前,先指定utf-8编码,如下: import csv import codecs if __name ...

  3. python写入csv文件的几种方法总结

    生成test.csv文件 #coding=utf- import pandas as pd #任意的多组列表 a = [,,] b = [,,] #字典中的key值即为csv中列名 dataframe ...

  4. python 写入csv文件

    import csv   fieldnames = ['Column1', 'Column2', 'Column3', 'Column4'] rows = [{'Column1': '0', 'Col ...

  5. Python写入csv文件示例

    import csv header = ['City', 'AQI', 'PM2.5/1h', 'PM10/1h', 'CO/1h', 'NO2/1h', 'O3/1h', 'O3/8h', 'SO2 ...

  6. python读取和写入csv文件

    读取csv文件: def readCsv(): rows=[] with file(r'E:\py\py01\Data\system.csv','rb') as f: reads=csv.reader ...

  7. 解决python中csv文件中文写入问题

    一.前言 一般来说,为了方便,使用python的时候都会使用csv模块去写数据到csv文件,但是写入中文的时候,经常会报错: UnicodeEncodeError: 'ascii' codec can ...

  8. python在不同情况下写入csv文件

    情况一(解法一):将列表存储为csv文件.列表的每一项代表csv文件的一行. 列表中的每一项包含多个属性.list=[[属性1,属性2,属性3,……],[属性1,属性2,属性3,……],[属性1,属性 ...

  9. python之读取和写入csv文件

    写入csv文件源码: #输出数据写入CSV文件 import csv data = [ ("Mike", "male", 24), ("Lee&quo ...

随机推荐

  1. quick cocos2d-x 下载地址

    https://github.com/chukong/quick-cocos2d-x/tree/master http://www.cocos2dx.net/post/280 配置说明 http:// ...

  2. PKU 1932 XYZZY(Floyd+Bellman||Spfa+Floyd)

    题目大意:原题链接 给你一张图,初始你在房间1,初始生命值为100,进入每个房间会加上那个房间的生命(可能为负),问是否能到达房间n.(要求进入每个房间后生命值都大于0) 解题思路: 解法一:Floy ...

  3. 好的博客参考之Spring

    https://blog.csdn.net/bao19901210/article/details/41724355

  4. Ubuntu16.04 Docker 安装

    前提条件 Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker. 通过 uname -r 命令查看你当前的内核版本 ...

  5. Python面试题之Python反射机制

    0x00 前言 def f1(): print('f1') def f2(): print('f2') def f3(): print('f3') def f4(): print('f4') a = ...

  6. SpringMvc接受特殊符号参数被转义

    WEB开发时,在前端通过get / post 方法传递参数的时候  如果实参附带特殊符号,后端接收到的值中特殊符号就会被转义 例如该请求: http://localhost:10001/demo/in ...

  7. SQL Server 2016 特性和安装方法

    SQL Server 2016 特性: 全程加密技术(Always Encrypted),动态数据屏蔽(Dynamic Data Masking),JSON支持,多TempDB数据库文件,PolyBa ...

  8. asp.net操作GridView添删改查的两种方法 及 光棒效果

    这部份小内容很想写下来了,因为是基础中的基础,但是近来用的比较少,又温习了一篇,发现有点陌生了,所以,还是写一下吧. 方法一:使用Gridview本身自带的事件处理,代码如下(注意:每次操作完都得重新 ...

  9. Java-性能调优实战(jps、jstack)

    找最耗CPU的线程 1. 找出java进程 [ ~]# jps 9939 Resin 9874 WatchdogManager 9926 Jps 2. 找java进程下所有的线程 [ ~]# top ...

  10. center os7.2 apache+php+mysql环境配置并设置https访问

    本人阿里云购买的center os7.2系统,小程序只支持https,因此需要配置https 安装apache yum -y install httpd systemctl start httpd a ...