欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python CSV模塊使用實(shí)例

 更新時(shí)間:2015年04月09日 10:38:20   投稿:junjie  
這篇文章主要介紹了Python CSV模塊使用實(shí)例,本文將舉幾個(gè)例子來(lái)介紹一下Python的CSV模塊的使用方法,包括reader、writer、DictReader、DictWriter.register_dialect等,需要的朋友可以參考下

舉幾個(gè)例子來(lái)介紹一下,Python 的 CSV模塊的使用方法,包括,reader, writer, DictReader, DictWriter.register_dialect

一直非常喜歡python的csv模塊,簡(jiǎn)單易用,經(jīng)常在項(xiàng)目中使用,現(xiàn)在舉幾個(gè)例子說(shuō)明一下。

復(fù)制代碼 代碼如下:

reader(csvfile[, dialect='excel'][, fmtparam])

參數(shù)表:

csvfile
        需要是支持迭代(Iterator)的對(duì)象,并且每次調(diào)用next方法的返回值是字符串(string),通常的文件(file)對(duì)象,或者列表(list)對(duì)象都是適用的,如果是文件對(duì)象,打開(kāi)是需要加"b"標(biāo)志參數(shù)。

dialect
        編碼風(fēng)格,默認(rèn)為excel方式,也就是逗號(hào)(,)分隔,另外csv模塊也支持excel-tab風(fēng)格,也就是制表符(tab)分隔。其它的方式需要自己定義,然后可以調(diào)用register_dialect方法來(lái)注冊(cè),以及l(fā)ist_dialects方法來(lái)查詢(xún)已注冊(cè)的所有編碼風(fēng)格列表。

fmtparam
        格式化參數(shù),用來(lái)覆蓋之前dialect對(duì)象指定的編碼風(fēng)格。

例子:

復(fù)制代碼 代碼如下:

import csv

reader = csv.reader(file('your.csv', 'rb'))
for line in reader:
    print line
 

writer(csvfile[, dialect='excel'][, fmtparam])


參數(shù)表(略: 同reader, 見(jiàn)上)

例子:

復(fù)制代碼 代碼如下:

import csv

writer = csv.writer(file('your.csv', 'wb'))
writer.writerow(['Column1', 'Column2', 'Column3'])
lines = [range(3) for i in range(5)]
for line in lines:
    writer.writerow(line)

DictReader

同reader差不多,都是讀取CSV用的,只不過(guò)會(huì)生成一個(gè)字典(dict)類(lèi)型的返回,而不是迭代類(lèi)型。

DictWriter

 我主要想說(shuō)的是DictWriter,我為什么會(huì)喜歡使用DictWriter呢,因?yàn)槠胀ǖ膚riter你需要手工去構(gòu)建列表,尤其是通過(guò)表單提交的時(shí)候,而我之前因?yàn)橐恢痹趜ope平臺(tái)上開(kāi)發(fā),而zope支持一種高級(jí)表單數(shù)據(jù)模型,也就是可以通過(guò)定義表單的時(shí)候加入相應(yīng)的標(biāo)志來(lái)使提交后的表單數(shù)據(jù)自動(dòng)的生成一個(gè)記錄(records)類(lèi)型,也就是生成一個(gè)每項(xiàng)數(shù)據(jù)都是一個(gè)字典的列表。這樣,我就可以非常方便的直接把表單數(shù)據(jù)傳給 DictWriter而生成csv,當(dāng)然這個(gè)是在你能保證數(shù)據(jù)的正確性的前提下。好下面我來(lái)簡(jiǎn)單的說(shuō)明一下這種zope的高級(jí)表單數(shù)據(jù)類(lèi)型。

例子:

復(fù)制代碼 代碼如下:

<form action='test_form_action' method=post>
            <input type="text" name="rows.Column1:records" value="0" />
            <input type="text" name="rows.Column2:records" value="1" />
            <input type="text" name="rows.Column3:records" value="2" />
            <input type="text" name="rows.Column4:records" value="3" />
    <br />
            <input type="text" name="rows.Column1:records" value="0" />
            <input type="text" name="rows.Column2:records" value="1" />
            <input type="text" name="rows.Column3:records" value="2" />
            <input type="text" name="rows.Column4:records" value="3" />
    <br />
            <input type="text" name="rows.Column1:records" value="0" />
            <input type="text" name="rows.Column2:records" value="1" />
            <input type="text" name="rows.Column3:records" value="2" />
            <input type="text" name="rows.Column4:records" value="3" />
    <br />
            <input type="text" name="rows.Column1:records" value="0" />
            <input type="text" name="rows.Column2:records" value="1" />
            <input type="text" name="rows.Column3:records" value="2" />
            <input type="text" name="rows.Column4:records" value="3" />
    <br />
            <input type="text" name="rows.Column1:records" value="0" />
            <input type="text" name="rows.Column2:records" value="1" />
            <input type="text" name="rows.Column3:records" value="2" />
            <input type="text" name="rows.Column4:records" value="3" />
    <br />
<input type="submit" value="Submit CSV" />
</form>

表單提交后的結(jié)果是:
復(fù)制代碼 代碼如下:

rows = [{'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'},
        {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'},
        {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'},
        {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'},
        {'Column1': '0', 'Column2': '1', 'Column3': '2', 'Column4': '3'}]

這樣就可以直接調(diào)用DictWriter.writerows方法來(lái)處理了:
復(fù)制代碼 代碼如下:

import csv

fieldnames = ['Column1', 'Column2', 'Column3', 'Column4']
dict_writer = csv.DictWriter(file('your.csv', 'wb'), fieldnames=fieldnames)
dict_writer.writerow(fieldnames) # CSV第一行需要自己加入
dict_writer.writerows(rows)  # rows就是表單提交的數(shù)據(jù)

*注意:這里的csv文件寫(xiě)入需要External Method的支持,因?yàn)樵趜ope中由于權(quán)限沙箱的問(wèn)題是不能直接操作csv模塊來(lái)讀寫(xiě)文件系統(tǒng)的。


 
這樣用起來(lái)是不是非常的方便呢,這里給出生成上面表單的DTML代碼:
復(fù)制代碼 代碼如下:

<form action='test_form' method=post>
<dtml-in "range(5)">
    <dtml-in "range(4)">
        <input type="text" name="rows.Column&dtml-sequence-number;:records" value="&dtml-sequence-item;" />
    </dtml-in>
<br />
</dtml-in>
<input type="submit" value="Submit CSV" />
</form>

您可以根據(jù)您自己的需要來(lái)改寫(xiě)這個(gè)表單的生成。

參考文獻(xiàn):
http://docs.python.org/lib/module-csv.html
http://www.python.org/dev/peps/pep-0305/

相關(guān)文章

最新評(píng)論