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

  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. python介绍和基础(待补充)

    python的介绍 把命令放到一个文件中,文件还能执行,这样的语言叫shell脚本 写一个c语言程序,.c结尾的,gcc运行c语言程序,生成.out文件,然后执行.out文件 c语言是先编写代码,再编 ...

  2. PL/SQL 表约束

    1. 表相关 a. 主键:constraint [主键的约束名] primary key b. 外键:constraint [外键约束名] foreign key( ) references []() ...

  3. maven tomcat7-maven-plugin配置及背景

    背景: 在研发阶段,想让一个服务通过tomcat启动起来有很多的方法,常用的idea都有这样的支持,那么如果我们没有tomcat,能不能让服务通过tomcat启动起来呢?maven就提供了这样的支持. ...

  4. log4j的配置和使用

    日志记录 在应用程序中添加日志记录总的来说基于三个目的: 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作: 跟踪代码运行时轨迹,作为日后审计的依据:担当集成开发环境中的调试器 ...

  5. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A. Andryusha and Socks

    地址:http://codeforces.com/contest/782/problem/A 题目: A. Andryusha and Socks time limit per test 2 seco ...

  6. [转]Unix/Linux开源世界资源链接汇总

    转自:http://blog.csdn.net/21aspnet/article/details/6754126 最后更新:2012.12.04 说明:好东西在后面,Linux镜像基本涵盖全球主要下载 ...

  7. 防止常见XSS 过滤 SQL注入 JAVA过滤器filter

    XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往W ...

  8. QML Image Element

    QML Image Element The Image element displays an image in a declarative user interface More... Image元 ...

  9. s3cmd安装

    配置yum.repos cd /etc/yum.repos.d/ vim s3tools.repo [s3tools] name=Tools for managing Amazon S3 - Simp ...

  10. powershell 设置环境变量 -- go 单元测试 exit status 3221225781

    执行单元测试时出错 go test -run TestImage 错误提示如下: exit status 3221225781 这个错误的意思是需要加载对应的库文件找不到,加载对应的库文件就习. 但是 ...