佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Python操作CSV格式文件的方法大全

Python操作CSV格式文件的方法大全

熱門標(biāo)簽:網(wǎng)站文章發(fā)布 檢查注冊(cè)表項(xiàng) 服務(wù)器配置 美圖手機(jī) 鐵路電話系統(tǒng) 呼叫中心市場(chǎng)需求 智能手機(jī) 銀行業(yè)務(wù)

(一)CSV格式文件

1.說(shuō)明

CSV是一種以逗號(hào)分隔數(shù)值的文件類型,在數(shù)據(jù)庫(kù)或電子表格中,常見的導(dǎo)入導(dǎo)出文件格式就是CSV格式,CSV格式存儲(chǔ)數(shù)據(jù)通常以純文本的方式存數(shù)數(shù)據(jù)表。

(二)CSV庫(kù)操作csv格式文本

操作一下表格數(shù)據(jù):

1.讀取表頭的2中方式

#方式一
import csv
with open("D:\\test.csv") as f:
    reader = csv.reader(f)
    rows=[row for row in  reader]
    print(rows[0])


----------
#方式二
import csv
with open("D:\\test.csv") as f:
    #1.創(chuàng)建閱讀器對(duì)象
    reader = csv.reader(f)
    #2.讀取文件第一行數(shù)據(jù)
    head_row=next(reader)
    print(head_row)

結(jié)果演示:['姓名', '年齡', '職業(yè)', '家庭地址', '工資']

2.讀取文件某一列數(shù)據(jù)

#1.獲取文件某一列數(shù)據(jù)
import csv
with 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 csv
with open("D:\\test.csv",'a') as f:
     row=['曹操','23','學(xué)生','黑龍江','5000']
     write=csv.writer(f)
     write.writerow(row)
     print("寫入完畢!")

結(jié)果演示:

4.獲取文件頭及其索引

import csv
with open("D:\\test.csv") as f:
    #1.創(chuàng)建閱讀器對(duì)象
    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 csv
with 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é)果演示:?jiǎn)T工最高工資為:10000

6.復(fù)制CSV格式文件

原文件test.csv

import csv
f=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)容:

(三)pandas庫(kù)操作CSV文件

csv文件內(nèi)容:

1.安裝pandas庫(kù):pip install pandas

2.讀取csv文件所有數(shù)據(jù)

 import pandas as pd
path= 'D:\\test.csv'
with open(path)as file:
    data=pd.read_csv(file)
    print(data)

結(jié)果演示:
      姓名  年齡   職業(yè)  家庭地址     工資
0     張三  22   廚師   北京市   6000
1     李四  26  攝影師  湖南長(zhǎng)沙   8000
2     王五  28  程序員    深圳  10000
3  Kaina  22   學(xué)生   黑龍江   2000
4     曹操  28   銷售    上海   6000

3.describe()方法數(shù)據(jù)統(tǒng)計(jì)

import pandas as pd
path= 'D:\\test.csv'
with open(path)as file:
    data=pd.read_csv(file)
    #了解更多describe()知識(shí),ctr+鼠標(biāo)左鍵
    print(data.describe())

結(jié)果演示:
             年齡            工資
count   5.00000      5.000000
mean   25.20000   6400.000000
std     3.03315   2966.479395
min    22.00000   2000.000000
25%    22.00000   6000.000000
50%    26.00000   6000.000000
75%    28.00000   8000.000000
max    28.00000  10000.000000

4.讀取文件前幾行數(shù)據(jù)

import pandas as pd
path= '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   廚師   北京市  6000
1  李四  26  攝影師  湖南長(zhǎng)沙  8000

5.讀取某一行所有數(shù)據(jù)

import pandas as pd
path= '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 pd
path= '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   廚師   北京市  6000
1     李四  26  攝影師  湖南長(zhǎng)沙  8000
3  Kaina  22   學(xué)生   黑龍江  2000

7.讀取所有行和列數(shù)據(jù)

import pandas as pd
path= 'D:\\test.csv'
with open(path)as file:
    data=pd.read_csv(file)
    #讀取所有行和列數(shù)據(jù)
    print(data.ix[:,:])

結(jié)果演示:
      姓名  年齡   職業(yè)  家庭地址     工資
0     張三  22   廚師   北京市   6000
1     李四  26  攝影師  湖南長(zhǎng)沙   8000
2     王五  28  程序員    深圳  10000
3  Kaina  22   學(xué)生   黑龍江   2000
4     曹操  28   銷售    上海   6000

8.讀取某一列的所有行數(shù)據(jù)

import pandas as pd
path= 'D:\\test.csv'
with open(path)as file:
    data=pd.read_csv(file)
    # print(data.ix[:, 4])
    print(data.ix[:,'工資'])

結(jié)果演示:
0     6000
1     8000
2    10000
3     2000
4     6000
Name: 工資, dtype: int64

9.讀取某幾列的某幾行

import pandas as pd
path= 'D:\\test.csv'
with open(path)as file:
    data=pd.read_csv(file)
    print(data.ix[[0,1,3],['姓名','職業(yè)','工資']])

結(jié)果演示:
      姓名   職業(yè)    工資
0     張三   廚師  6000
1     李四  攝影師  8000
3  Kaina   學(xué)生  2000

10.讀取某一行和某一列對(duì)應(yīng)的數(shù)據(jù)

import pandas as pd
path= '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.csv
data.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 pd
file=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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python寫入CSV文件的方法
  • Python將列表數(shù)據(jù)寫入文件(txt, csv,excel)
  • python對(duì)csv文件追加寫入列的方法
  • python讀寫csv文件方法詳細(xì)總結(jié)
  • python讀取csv文件示例(python操作csv)
  • python寫入數(shù)據(jù)到csv或xlsx文件的3種方法
  • 利用Python如何將數(shù)據(jù)寫到CSV文件中
  • Python使用pandas處理CSV文件的實(shí)例講解
  • Python操作csv文件實(shí)例詳解
  • Python實(shí)現(xiàn)讀取及寫入csv文件的方法示例

標(biāo)簽:紅河 沈陽(yáng) 新疆 樂山 上海 滄州 長(zhǎng)治 河南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python操作CSV格式文件的方法大全》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    中山市| 昭觉县| 绥阳县| 苏尼特右旗| 秭归县| 怀化市| 泗水县| 滦平县| 新宁县| 武城县| 长武县| 长沙市| 永丰县| 和硕县| 盘锦市| 永州市| 达孜县| 台南市| 宣武区| 怀安县| 鄂伦春自治旗| 攀枝花市| 巴东县| 岳阳县| 婺源县| 龙海市| 朔州市| 井陉县| 华宁县| 龙里县| 洪洞县| 灵寿县| 开远市| 丹阳市| 和平区| 北川| 荔波县| 杭锦后旗| 太湖县| 慈溪市| 繁昌县|