• Python CSV模块处理文件读写


    下面是一个简单的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
     

    使用csv模块处理

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

    可以使用for-in循环逐条访问reader中的list类型元素,使用csv模块非常智能,可以处理元素内含逗号的复杂csv文件。reader.line_num记录着目前读到第几行。


    处理换行符 

    有一点要注意的是,如果用excel文件另存为csv文件,读行尾符号可能会遇到问题

    Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

    这时候下面这种方式打开文件就没有问题啦

    csv.reader(open(filename,"rU")) 

    改变分隔符

    创建一csv.excel的子类,并修改分隔符为”;”

    # File: csv-example-2.py
    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参数中设置,如下:

    # File: csv-example-3.py
     
    import csv
     
    reader = csv.reader(open("samples/sample.skv"), delimiter=";")
     
    for title, year, director in reader:
        print year, title


    将数据存为CSV格式

    通过csv.writer来生成一csv文件。

    # File: csv-example-4.py
     
    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)
    # writer = csv.writer(open("out.csv","w"))
      for item in data: writer.writerow(item)
     

    参考:

    http://www.pythonclub.org/python-files/csv

    http://stackoverflow.com/questions/2930673/python-and-csv-help

  • 相关阅读:
    Jenkins插件管理及汉化
    rpm安装Jenkins报错
    Codeforces Round #572 (Div. 2) A.
    [kuangbin带你飞]专题一 简单搜索 A棋盘问题
    北京信息科技大学第十一届程序设计竞赛(重现赛)I
    北京信息科技大学第十一届程序设计竞赛(重现赛)H
    北京信息科技大学第十一届程序设计竞赛(重现赛)B
    nyoj 206-矩形的个数 (a*b*(a+1)*(b+1)/4)
    nyoj 241-字母统计 (python count)
    nyoj 242-计算球体积 (pi*r*r*r*4/3)
  • 原文地址:https://www.cnblogs.com/wei-li/p/3549520.html
Copyright © 2020-2023  润新知