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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM