Python CSV文件處理/讀寫及With as 用法


可以不使用CSV模塊

逐行處理:

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

將數據存為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)

怎么獲取csv reader 對象的len?
參考:http://stackoverflow.com/questions/2890549/number-of-lines-in-csv-dictreader

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

I've gotten used to generating CSV (comma separated value) files in Python for these folks. Ever so often, someone sends me a CSV file they have created via Excel. Invariably, I will get the following error when trying to read that file with Python:

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

This is annoying, but the solution to the problem is to open the file with universal newline mode enabled. This means using the mode 'U' or 'rU' in your open() function call. For example:

reader = csv.reader(open("data.csv", 'rU'), dialect='excel')

According to the open() documentation, universal newline mode accepts \n,\r, and \r\n as valid newline characters.

 

 


python with用法:
with是python2.5以后才有的,它實質是一個控制流語句,with可以用來簡化try-finally語句。它的主要用法是實現一個類__enter__()和__exit__()方法,基本形式如下
class controlled_execution:
    def _enter__(self):
        set things up
        return thing
    def __exit__(self, type, value,  traceback):
        tear thing down
with controlled_execution() as thing: some code


   在實際的運行過程中,python會首先運行enter里的代碼,返回thing,作為as 后面的變量值,然后再運行with模塊中的代碼,最后會自動執行exit中的代碼,而不管with中的代碼運行結果如何。這也就是with能簡化try-finally語句的原因。所以with通常用在讀取文件的操作中,將文件句柄的關閉操作放在exit方法中,這樣就不會因忘記釋放文件句柄而產生可能出現的錯誤。

     另外,exit()方法的返回值可以用來指示with部分的代碼出現的異常是否需要raise,如果返回false,則會raise,否則,不進行任何操作。

 try、raise 陳述句 中有個讀取檔案的範例:

file = open('demo.py', 'r', encoding='UTF-8')
try:
for line in file:
print(line, end='')
except:
print('讀取檔案發生錯誤')
finally:
file.close()


為了要處理檔案讀取過程中發生的例外,並且最後確定檔案一定會關閉,你使用了try..except...finally語句,實際上,在Python 3(或2.6)中,你可以使用with as語句來簡化程式的撰寫:

with open('demo.py', 'r', encoding='UTF-8') as file:
for line in file:
print(line, end='')


with之後的運算式傳回的物件,可以使用as指定給變數來參考,在上面的例子中,file所參考到的物件,最後會被自動關閉,即使在with as的區塊中發生了例外,最後一定會關閉file所參考的物件。

實際上,只要物件支援環境管理協定(Context Management Protocol),就可以使用with as語句。支援環境管理協定的物件,必須實作__enter__()__exit__()兩個方法,這樣的物件稱之為環境管理員(Context Manager)

with陳述句一開始執行,就會進行__enter__()方法,該方法傳回的物件,可以使用as指定給變數(如果有的話),接著就執行with區塊中的程式碼。

如果with區塊中的程式碼發生了例外,則會執行__exit__()方法,並傳入三個引數,這三個引數,與 
再看 try、raise 中所提到的 
sys.exc_info() 傳回的三個值是相同的,也就是例外的類型、例 外訊息以及traceback物件。此時__exit__()方法若傳回False,則例外會被重新丟出,否則例外就停止傳播,通常__exit__()會傳回False以在with之外還可以處理例外。

如果with區塊中沒有發生例外而執行完畢,則也是執行__exit__()方法,此時__exit__()的三個參數都接收到None

所以,假設你要自行實作一個可以自動關閉檔案的物件,則可以如下:

class FileReader:
def __init__(self, filename):
self.filename = filename

def __enter__(self):
self.file = open(self.filename, 'r', encoding='UTF-8')
return self.file

def __exit__(self, type, msg, traceback):
if type:
print(msg) # 作你的例外處理
self.file.close()
return False


接下來你就可以在FileReader物件上使用with as語句。例如:

with FileReader('demo.py') as file:
    for line in file:
        print(line, end='')

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

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


免責聲明!

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



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