使用Python读取/导出(写入)CSV文件

想要用python处理csv文件。

去查了下,python中本身就自带csv模块。

然后参考在线手册:

http://docs.python.org/2/library/csv.html

去试试。

【用python生成csv】

1. 按照手册的例子,试了试:

?
1
2
3
4
5
6
import csv
with open ( 'eggs.csv' , 'wb' ) as csvfile:
     spamwriter = csv.writer(csvfile, delimiter = ' ' ,
                             quotechar = '|' , quoting = csv.QUOTE_MINIMAL)
     spamwriter.writerow([ 'Spam' ] * 5 + [ 'Baked Beans' ])
     spamwriter.writerow([ 'Spam' , 'Lovely Spam' , 'Wonderful Spam' ])

然后生成的文件,原始数据为:

?
1
2
Spam Spam Spam Spam Spam |Baked Beans|
Spam |Lovely Spam| |Wonderful Spam|

然后用excel打开结果为:

csv effect orig

很明显,不是想要的结果,因为只是一列,实际应该是6列才对。

2. 然后折腾了一下之后,参考手册的说明:

csv. writer ( csvfile[,  dialect=’excel’][,  fmtparam] )

Return a writer object responsible for converting the user’s data into delimited strings on the given file-like object. csvfile can be any object with a write() method. If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference. An optional dialect parameter can be given which is used to define a set of parameters specific to a particular CSV dialect. It may be an instance of a subclass of the Dialect class or one of the strings returned by the list_dialects() function. The other optional fmtparam keyword arguments can be given to override individual formatting parameters in the current dialect. For full details about the dialect and formatting parameters, see section Dialects and Formatting Parameters. To make it as easy as possible to interface with modules which implement the DB API, the value None is written as the empty string. While this isn’t a reversible transformation, it makes it easier to dump SQL NULL data values to CSV files without preprocessing the data returned from a cursor.fetch* call. All other non-string data are stringified with str() before being written.

去改为:

?
1
2
3
4
5
6
import csv
with open ( 'eggs.csv' , 'wb' ) as csvfile:
     #spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
     spamwriter = csv.writer(csvfile, dialect = 'excel' )
     spamwriter.writerow([ 'Spam' ] * 5 + [ 'Baked Beans' ])
     spamwriter.writerow([ 'Spam' , 'Lovely Spam' , 'Wonderful Spam' ])

然后就可以显示正常了:

show ok in excel

去看了下源码为:

?
1
2
Spam,Spam,Spam,Spam,Spam,Baked Beans
Spam,Lovely Spam,Wonderful Spam

 

【总结】

使用python的csv生成excel所兼容的csv文件的话,主要就是创建writer时的参数时要有dialect=’excel’,就可以了。

 

【用Python读取从excel导出的csv文件】

再去尝试用python处理,从excel 2010导出的一个csv文件:

excel 2010 ms-dos csv test

参考手册的代码,使用如下代码:

?
1
2
3
4
5
6
import csv
with open ( 'excel_2010_ms-dos.csv' , 'rb' ) as csvfile:
     #spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
     spamreader = csv.reader(csvfile, dialect = 'excel' )
     for row in spamreader:
         print ', ' .join(row)

去测试,然后是可以正常打印出结果的:

?
1
2
3
4
D:\tmp\tmp_dev_root\python\test_csv>test_csv.py
a1, b1, c1, d1
a2, , c2,
, b3, ,

【总结】

只要指定了对应的dialect=’excel’,也是可以很方便的使用python的csv处理excel所导出的ms-dos的csv文件的。

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值