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

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

 更新時間:2021年07月14日 15:49:41   作者:凱耐  
CSV 文件(Comma Separated Values file,即逗號分隔值文件)是一種純文本文件,它使用特定的結構來排列表格數(shù)據(jù),這篇文章主要給大家介紹了關于Python操作CSV格式文件的相關資料,需要的朋友可以參考下

(一)CSV格式文件

1.說明

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

(二)CSV庫操作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)建閱讀器對象
    reader = csv.reader(f)
    #2.讀取文件第一行數(shù)據(jù)
    head_row=next(reader)
    print(head_row)

結果演示:['姓名', '年齡', '職業(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)

結果演示:['姓名', '張三', '李四', '王五', 'Kaina']

3.向csv文件中寫入數(shù)據(jù)

#1.向csv文件中寫入數(shù)據(jù)
import csv
with open("D:\\test.csv",'a') as f:
     row=['曹操','23','學生','黑龍江','5000']
     write=csv.writer(f)
     write.writerow(row)
     print("寫入完畢!")

結果演示:

這里寫圖片描述

4.獲取文件頭及其索引

import csv
with 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)

結果演示:
['姓名', '年齡', '職業(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)))

結果演示:員工最高工資為:10000

6.復制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.未添加關鍵字參數(shù)newline=' '的結果:

這里寫圖片描述

02添加關鍵字參數(shù)newline=' '的Aim.csv文件的內容:

這里寫圖片描述

(三)pandas庫操作CSV文件

csv文件內容:

這里寫圖片描述

1.安裝pandas庫: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)

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

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

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

結果演示:
             年齡            工資
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)


結果演示:
   姓名  年齡   職業(yè)  家庭地址    工資
0  張三  22   廚師   北京市  6000
1  李四  26  攝影師  湖南長沙  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,])


結果演示:
姓名        張三
年齡        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],:])


結果演示:
      姓名  年齡   職業(yè)  家庭地址    工資
0     張三  22   廚師   北京市  6000
1     李四  26  攝影師  湖南長沙  8000
3  Kaina  22   學生   黑龍江  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[:,:])

結果演示:
      姓名  年齡   職業(yè)  家庭地址     工資
0     張三  22   廚師   北京市   6000
1     李四  26  攝影師  湖南長沙   8000
2     王五  28  程序員    深圳  10000
3  Kaina  22   學生   黑龍江   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[:,'工資'])

結果演示:
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è)','工資']])

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

10.讀取某一行和某一列對應的數(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])

結果演示:職業(yè)---程序員

11.CSV數(shù)據(jù)的導入導出(復制CSV文件)

讀方式01:

import pandas as pd
#1.讀入數(shù)據(jù)
data=pd.read_csv(file)

寫出數(shù)據(jù)02:

import pandas as pd
#1.寫出數(shù)據(jù),目標文件是Aim.csv
data.to_csv('Aim.csv')

其他:

01.讀取網絡數(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)

實例演示:

1.test.csv原文件內容

這里寫圖片描述

2.現(xiàn)在把test.csv中的內容復制到Aim.csv中

import pandas as pd
file=open('test.csv')
#1.讀取file中的數(shù)據(jù)
data=pd.read_csv(file)
#2.把data寫到目標文件Aim.csv中
data.to_csv('Aim.csv')
print(data)

結果演示:

這里寫圖片描述

注:pandas模塊處理Excel文件和處理CSV文件差不多!

參考文檔:https://docs.python.org/3.6/library/csv.html

總結

到此這篇關于Python操作CSV格式文件的文章就介紹到這了,更多相關Python操作CSV文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python標準庫os庫的函數(shù)介紹

    python標準庫os庫的函數(shù)介紹

    這篇文章主要介紹了python標準庫os庫的函數(shù)介紹,需要的朋友可以參考下
    2020-02-02
  • 完美解決在oj中Python的循環(huán)輸入問題

    完美解決在oj中Python的循環(huán)輸入問題

    今天小編就為大家分享一篇完美解決在oj中Python的循環(huán)輸入問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • matplotlib相關系統(tǒng)目錄獲取方式小結

    matplotlib相關系統(tǒng)目錄獲取方式小結

    這篇文章主要介紹了matplotlib相關系統(tǒng)目錄獲取方式小結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • Python腳本導出為exe程序的方法

    Python腳本導出為exe程序的方法

    這篇文章主要介紹了如何把Python腳本導出為exe程序的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 淺談python中的變量默認是什么類型

    淺談python中的變量默認是什么類型

    python中的變量默認是什么類型呢?還有很多新手不太明白。下面小編就為大家介紹一下python中的變量默認是什么類型。一起跟隨小編過來看看吧
    2016-09-09
  • 初學者快看,Python下劃線的五個作用介紹

    初學者快看,Python下劃線的五個作用介紹

    大家好,本篇文章主要講的是初學者快看,Python下劃線的五個作用介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Python如何有效地使用迭代

    Python如何有效地使用迭代

    這篇文章主要為大家詳細介紹了Python如何有效地使用迭代,文中的示例代碼講解詳細,對我們深入了解Python有一定的幫助,需要的小伙伴可以學習一下
    2023-09-09
  • Python從入門到精通之多線程使用詳解

    Python從入門到精通之多線程使用詳解

    這篇文章主要介紹了Python中的多線程使用,包括創(chuàng)建線程、線程同步、線程間通信以及線程池等基本概念和技巧,文中的示例代碼講解詳細,需要的可以參考一下
    2023-07-07
  • 解決Django模板無法使用perms變量問題的方法

    解決Django模板無法使用perms變量問題的方法

    這篇文章主要給大家介紹了關于解決Django模板無法使用perms變量問題的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-09-09
  • 淺談Series和DataFrame中的sort_index方法

    淺談Series和DataFrame中的sort_index方法

    今天小編就為大家分享一篇淺談Series和DataFrame中的sort_index方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論