python标准库:csv 模块

原文地址:http://www.bugingcode.com/blog/python_csv.html

csv 模块被用来读取CSV格式(用逗号分割数值)的数据文件,CSV格式的文件经常在微软的Excel和其他的程序中使用到。一个CSV文件经常包含一些行,每行包含一些列,列和列之前通常使用逗号分隔。

如下的例子:

Title,Release Date,Director
And Now For Something Completely Different,1971,Ian MacNaughton
Monty Python And The Holy Grail,1975,Terry Gilliam and Terry Jones
Monty Python's Life Of Brian,1979,Terry Jones
Monty Python Live At The Hollywood Bowl,1982,Terry Hughes
Monty Python's The Meaning Of Life,1983,Terry Jones

读取这个文件,你可是简单的循环每一行,使用split方法得到每一个单独的列:

for line in open("samples/sample.csv"):
    title, year, director = line.split(",")
    print year, title

使用csv模块取代这种方法,你可以这样写:

import csv

reader = csv.reader(open("samples/sample.csv"))

for title, year, director in reader:
    print year, title

代码如下:

$ python csv-example-1.py
Release Date Title
1971 And Now For Something Completely Different
1975 Monty Python And The Holy Grail
1979 Monty Python's Life Of Brian
1982 Monty Python Live At The Hollywood Bowl
1983 Monty Python's The Meaning Of Life

reader函数传入一个序列或者是一个文件对象的迭代器,返回一个迭代器。

csv模块的解析器不仅仅对字符串进行分割。他处理引用值和适当的行结束符,因此你能够读入包含随意的字符串(允许字符串包含逗号)。他也是可以进行配置的,你可以创建和注册一个专有的描述符,用它去读取不同的CSV特性。

如下的例子,介绍如何读取瑞典变种的CSV,它使用分号取代了逗号进行分割。

import csv

class SKV(csv.excel):
    # like excel, but uses semicolons
    delimiter = ";"

csv.register_dialect("SKV", SKV)

reader = csv.reader(open("samples/sample.skv"), "SKV")

for title, year, director in reader:
    print year, title

值得注意的是如果你仅仅想要修改一些参数的话,你可以用关键参数传入reader函数:

import csv

reader = csv.reader(open("samples/sample.skv"), delimiter=";")

for title, year, director in reader:
    print year, title

这个模块同样被用来创建CSV文件。writer函数创建CSV写对象,它可以方便的把数值转换为字符串,并在合适的位置跳出。

import csv
import sys

data = [
    ("And Now For Something Completely Different", 1971, "Ian MacNaughton"),
    ("Monty Python And The Holy Grail", 1975, "Terry Gilliam, Terry Jones"),
    ("Monty Python's Life Of Brian", 1979, "Terry Jones"),
    ("Monty Python Live At The Hollywood Bowl", 1982, "Terry Hughes"),
    ("Monty Python's The Meaning Of Life", 1983, "Terry Jones")
]

writer = csv.writer(sys.stdout)

for item in data:
    writer.writerow(item)
$ python csv-example-4.py
And Now For Something Completely Different,1971,Ian MacNaughton
Monty Python And The Holy Grail,1975,"Terry Gilliam, Terry Jones"
Monty Python's Life Of Brian,1979,Terry Jones
Monty Python Live At The Hollywood Bowl,1982,Terry Hughes
Monty Python's The Meaning Of Life,1983,Terry Jones

如果你有一个序列的数据(或者一个迭代器对象),你可以使用writerows方法代替:

writer = csv.writer(sys.stdout)
writer.writerows(data)

这个writer函数也支持不同的自定义格式。

转载请标明来之:阿猫学编程

更多教程:阿猫学编程-python基础教程

posted on 2018-04-03 15:49  go2coding  阅读(308)  评论(0编辑  收藏  举报

导航