這篇文章給大家分享的是有關(guān)python中不同的CSV功能和使用示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供武宣網(wǎng)站建設(shè)、武宣做網(wǎng)站、武宣網(wǎng)站設(shè)計(jì)、武宣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、武宣企業(yè)網(wǎng)站模板建站服務(wù),10余年武宣做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
1、云計(jì)算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運(yùn)維工程項(xiàng)目,自動(dòng)化運(yùn)維的標(biāo)配就是python+Django/flask。5、金融理財(cái)分析,量化交易,金融分析。6、大數(shù)據(jù)分析。
一、CSV模塊功能
在CSV模塊下,可以找到以下功能

二、Python中CSV文件操作
加載CSV文件后,您可以執(zhí)行多種操作。將在Python中顯示對(duì)CSV文件的讀取和寫入操作
在Python中讀取CSV文件:
import csv
with open('Titanic.csv','r') as csv_file: #Opens the file in read mode
csv_reader = csv.reader(csv_file) # Making use of reader method for reading the file
for line in csv_reader: #Iterate through the loop to read line by line
print(line)用Python寫入CSV文件:
import csv
with open('Titanic.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
with open('new_Titanic.csv', 'w') as new_file: # Open a new file named 'new_titanic.csv' under write mode
csv_writer = csv.writer(new_file, delimiter=';') #making use of write method
for line in csv_reader: # for each file in csv_reader
csv_writer.writerow(line) #writing out to a new file from each line of the original file讀取CSV文件作為字典
import csv
with open('Titanic.csv','r') as csv_file: #Open the file in read mode
csv_reader = csv.DictReader(csv_file) #use dictreader method to reade the file in dictionary
for line in csv_reader: #Iterate through the loop to read line by line
print(line)作為字典寫入CSV文件
import csv
mydict = [{'Passenger':'1', 'Id':'0', 'Survived':'3'}, #key-value pairs as dictionary obj
{'Passenger':'2', 'Id':'1', 'Survived':'1'},
{'Passenger':'3', 'Id':'1', 'Survived':'3'}]
fields = ['Passenger', 'Id', 'Survived'] #field names
filename = 'new_Titanic.csv' #name of csv file
with open('new_Titanic.csv', 'w')as new_csv_file: #open a new file 'new_titanic,csv' under write mode
writer = csv.DictWriter(new_csv_file, fieldnames=fields)
writer.writeheader() #writing the headers(field names)
writer.writerows(mydict) #writing data rows感謝各位的閱讀!關(guān)于“python中不同的CSV功能和使用示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文題目:python中不同的CSV功能和使用示例分析
網(wǎng)頁網(wǎng)址:http://chinadenli.net/article48/pgjoep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、云服務(wù)器、虛擬主機(jī)、響應(yīng)式網(wǎng)站、定制開發(fā)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)