Python CSV模塊使用實(shí)例
舉幾個(gè)例子來(lái)介紹一下,Python 的 CSV模塊的使用方法,包括,reader, writer, DictReader, DictWriter.register_dialect
一直非常喜歡python的csv模塊,簡(jiǎn)單易用,經(jīng)常在項(xiàng)目中使用,現(xiàn)在舉幾個(gè)例子說(shuō)明一下。
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)格。
例子:
import csv
reader = csv.reader(file('your.csv', 'rb'))
for line in reader:
print line
writer(csvfile[, dialect='excel'][, fmtparam])
參數(shù)表(略: 同reader, 見(jiàn)上)
例子:
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)型。
例子:
<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é)果是:
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)處理了:
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代碼:
<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)文章
如何利用Python快速繪制海報(bào)級(jí)別地圖詳解
Python之所以這么流行,是因?yàn)樗粌H能夠應(yīng)用于科技領(lǐng)域,還能用來(lái)做許多其他學(xué)科的研究工具,最常見(jiàn)的便是繪制地圖,這篇文章主要給大家介紹了關(guān)于如何利用Python快速繪制海報(bào)級(jí)別地圖的相關(guān)資料,需要的朋友可以參考下2021-09-09python之ImportError:模塊引入異常問(wèn)題
這篇文章主要介紹了python之ImportError:模塊引入異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06python機(jī)器學(xué)習(xí)Logistic回歸原理推導(dǎo)
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)Logistic回歸原理推導(dǎo),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Django框架model模型對(duì)象驗(yàn)證實(shí)現(xiàn)方法分析
這篇文章主要介紹了Django框架model模型對(duì)象驗(yàn)證實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Django框架model模型對(duì)象驗(yàn)證相關(guān)原理、實(shí)現(xiàn)步驟及操作注意事項(xiàng),需要的朋友可以參考下2019-10-10python django事務(wù)transaction源碼分析詳解
這篇文章主要介紹了python django事務(wù)transaction源碼分析詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03Python2.7實(shí)現(xiàn)多進(jìn)程下開(kāi)發(fā)多線(xiàn)程示例
這篇文章主要為大家詳細(xì)介紹了Python2.7實(shí)現(xiàn)多進(jìn)程下開(kāi)發(fā)多線(xiàn)程示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05python list count統(tǒng)計(jì)個(gè)數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了python list count統(tǒng)計(jì)個(gè)數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02python按照行來(lái)讀取txt文件全部?jī)?nèi)容(去除空行處理掉\t,\n后以列表方式返回)
這篇文章主要介紹了python按照行來(lái)讀取txt文件全部?jī)?nèi)容 ,去除空行,處理掉\t,\n后,以列表方式返回,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06