Python操作CSV格式文件的方法大全
1.說明
CSV是一種以逗號分隔數(shù)值的文件類型,在數(shù)據(jù)庫或電子表格中,常見的導(dǎo)入導(dǎo)出文件格式就是CSV格式,CSV格式存儲數(shù)據(jù)通常以純文本的方式存數(shù)數(shù)據(jù)表。
(二)CSV庫操作csv格式文本操作一下表格數(shù)據(jù):
1.讀取表頭的2中方式
#方式一import csvwith open('D:test.csv') as f: reader = csv.reader(f) rows=[row for row in reader] print(rows[0])----------#方式二import csvwith open('D:test.csv') as f: #1.創(chuàng)建閱讀器對象 reader = csv.reader(f) #2.讀取文件第一行數(shù)據(jù) head_row=next(reader) print(head_row)
結(jié)果演示:[’姓名’, ’年齡’, ’職業(yè)’, ’家庭地址’, ’工資’]
2.讀取文件某一列數(shù)據(jù)
#1.獲取文件某一列數(shù)據(jù)import csvwith open('D:test.csv') as f: reader = csv.reader(f) column=[row[0] for row in reader] print(column)
結(jié)果演示:[’姓名’, ’張三’, ’李四’, ’王五’, ’Kaina’]
3.向csv文件中寫入數(shù)據(jù)
#1.向csv文件中寫入數(shù)據(jù)import csvwith open('D:test.csv',’a’) as f: row=[’曹操’,’23’,’學(xué)生’,’黑龍江’,’5000’] write=csv.writer(f) write.writerow(row) print('寫入完畢!')
結(jié)果演示:
4.獲取文件頭及其索引
import csvwith open('D:test.csv') as f: #1.創(chuàng)建閱讀器對象 reader = csv.reader(f) #2.讀取文件第一行數(shù)據(jù) head_row=next(reader) print(head_row) #4.獲取文件頭及其索引 for index,column_header in enumerate(head_row):print(index,column_header)
結(jié)果演示:[’姓名’, ’年齡’, ’職業(yè)’, ’家庭地址’, ’工資’]0 姓名1 年齡2 職業(yè)3 家庭地址4 工資
5.獲取某列的最大值
# [’姓名’, ’年齡’, ’職業(yè)’, ’家庭地址’, ’工資’]import csvwith open('D:test.csv') as f: reader = csv.reader(f) header_row=next(reader) # print(header_row) salary=[] for row in reader:#把第五列數(shù)據(jù)保存到列表salary中 salary.append(int(row[4])) print(salary) print('員工最高工資為:'+str(max(salary)))
結(jié)果演示:員工最高工資為:10000
6.復(fù)制CSV格式文件
原文件test.csv
import csvf=open(’test.csv’)#1.newline=’’消除空格行aim_file=open(’Aim.csv’,’w’,newline=’’)write=csv.writer(aim_file)reader=csv.reader(f)rows=[row for row in reader]#2.遍歷rows列表for row in rows: #3.把每一行寫到Aim.csv中 write.writerow(row)
01.未添加關(guān)鍵字參數(shù)newline=’ ’的結(jié)果:
02添加關(guān)鍵字參數(shù)newline=’ ’的Aim.csv文件的內(nèi)容:
csv文件內(nèi)容:
1.安裝pandas庫:pip install pandas
2.讀取csv文件所有數(shù)據(jù)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) print(data)
結(jié)果演示: 姓名 年齡 職業(yè) 家庭地址 工資0 張三 22 廚師 北京市 60001 李四 26 攝影師 湖南長沙 80002 王五 28 程序員 深圳 100003 Kaina 22 學(xué)生 黑龍江 20004 曹操 28 銷售 上海 6000
3.describe()方法數(shù)據(jù)統(tǒng)計(jì)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) #了解更多describe()知識,ctr+鼠標(biāo)左鍵 print(data.describe())
結(jié)果演示: 年齡 工資count 5.00000 5.000000mean 25.20000 6400.000000std 3.03315 2966.479395min 22.00000 2000.00000025% 22.00000 6000.00000050% 26.00000 6000.00000075% 28.00000 8000.000000max 28.00000 10000.000000
4.讀取文件前幾行數(shù)據(jù)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) #讀取前2行數(shù)據(jù) # head_datas = data.head(0) head_datas=data.head(2) print(head_datas)
結(jié)果演示: 姓名 年齡 職業(yè) 家庭地址 工資0 張三 22 廚師 北京市 60001 李四 26 攝影師 湖南長沙 8000
5.讀取某一行所有數(shù)據(jù)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) #讀取第一行所有數(shù)據(jù) print(data.ix[0,])
結(jié)果演示:姓名張三年齡22職業(yè)廚師家庭地址 北京市工資 6000
6.讀取某幾行的數(shù)據(jù)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) #讀取第一行、第二行、第四行的所有數(shù)據(jù) print(data.ix[[0,1,3],:])
結(jié)果演示: 姓名 年齡 職業(yè) 家庭地址 工資0 張三 22 廚師 北京市 60001 李四 26 攝影師 湖南長沙 80003 Kaina 22 學(xué)生 黑龍江 2000
7.讀取所有行和列數(shù)據(jù)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) #讀取所有行和列數(shù)據(jù) print(data.ix[:,:])
結(jié)果演示: 姓名 年齡 職業(yè) 家庭地址 工資0 張三 22 廚師 北京市 60001 李四 26 攝影師 湖南長沙 80002 王五 28 程序員 深圳 100003 Kaina 22 學(xué)生 黑龍江 20004 曹操 28 銷售 上海 6000
8.讀取某一列的所有行數(shù)據(jù)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) # print(data.ix[:, 4]) print(data.ix[:,’工資’])
結(jié)果演示:0 60001 80002 100003 20004 6000Name: 工資, dtype: int64
9.讀取某幾列的某幾行
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) print(data.ix[[0,1,3],[’姓名’,’職業(yè)’,’工資’]])
結(jié)果演示: 姓名 職業(yè) 工資0 張三 廚師 60001 李四 攝影師 80003 Kaina 學(xué)生 2000
10.讀取某一行和某一列對應(yīng)的數(shù)據(jù)
import pandas as pdpath= ’D:test.csv’with open(path)as file: data=pd.read_csv(file) #讀取第三行的第三列 print('職業(yè)---'+data.ix[2,2])
結(jié)果演示:職業(yè)---程序員
11.CSV數(shù)據(jù)的導(dǎo)入導(dǎo)出(復(fù)制CSV文件)
讀方式01:
import pandas as pd#1.讀入數(shù)據(jù)data=pd.read_csv(file)
寫出數(shù)據(jù)02:
import pandas as pd#1.寫出數(shù)據(jù),目標(biāo)文件是Aim.csvdata.to_csv(’Aim.csv’)
其他:
01.讀取網(wǎng)絡(luò)數(shù)據(jù):import pandas as pd data_url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv'#填寫url讀取df = pd.read_csv(data_url)----------02.讀取excel文件數(shù)據(jù)import pandas as pd data = pd.read_excel(filepath)
實(shí)例演示:
1.test.csv原文件內(nèi)容
2.現(xiàn)在把test.csv中的內(nèi)容復(fù)制到Aim.csv中
import pandas as pdfile=open(’test.csv’)#1.讀取file中的數(shù)據(jù)data=pd.read_csv(file)#2.把data寫到目標(biāo)文件Aim.csv中data.to_csv(’Aim.csv’)print(data)
結(jié)果演示:
注:pandas模塊處理Excel文件和處理CSV文件差不多!
參考文檔:https://docs.python.org/3.6/library/csv.html
總結(jié)到此這篇關(guān)于Python操作CSV格式文件的文章就介紹到這了,更多相關(guān)Python操作CSV文件內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python web框架的總結(jié)2. Python如何進(jìn)行時(shí)間處理3. 詳解Python模塊化編程與裝飾器4. python使用ctypes庫調(diào)用DLL動態(tài)鏈接庫5. html小技巧之td,div標(biāo)簽里內(nèi)容不換行6. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式7. python logging 重復(fù)寫日志問題解決辦法詳解8. Python中l(wèi)ogger日志模塊詳解9. python裝飾器三種裝飾模式的簡單分析10. Python實(shí)現(xiàn)迪杰斯特拉算法過程解析
