python3讀取csv檔案任意行列。

2021-09-23 13:18:55 字數 1845 閱讀 9219

enumerate() 函式用於將乙個可遍歷的資料物件(如列表、元組或字串)組合為乙個索引序列,同時列出資料和資料下標

filename='d:/data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=f.readlines()

for index,info in enumerate(read):

if index ==0: #這裡判斷

#這裡輸出的是字串型別

print(info)

filename='data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=f.readlines()

for index,info in enumerate(read):

if index !=0: #這裡判斷

print(info)

filename='data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=f.readlines()

for index,info in enumerate(read):

if index <10: #這裡為索引,是 int 整形

print(info)

filename='data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=f.readlines()

for index,info in enumerate(read):

print(index) #自己根據index的數字判斷

import csv

filename='data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

#這裡輸出的是列表型別

print(info[:2]) #[:2]代表的是讀取第0列和第1列 ,第2列不包括

import csv

filename='data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

if index!=0: #這裡加判斷

print(info[:2])

import csv

filename='data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

print(info[-2:])

filename='data.csv'

with open(filename,'r',encoding='utf-8')as f:

read=csv.reader(f)

for index,info in enumerate(read):

if index==0:

print(info[2:3])

Python3中讀取寫入csv檔案

import csv 讀取資料 file path d 1csvread.csv with open file path,encoding utf 8 as f reader csv.reader f result list reader reader 方法返回物件,需要先轉型成list才能看到資料...

python3基礎 讀取和寫入csv檔案

city password day 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 import csv 開啟乙個csv檔案,模式為讀取 csvfile open users yanghui study python example.csv r 定義乙個變數,進行讀取 readcsv c...

python3讀取csv和xlsx檔案的例項

基於win10系統,python3.6 讀取csv 使用csv函式包,安裝 pip install csv 使用方法 import csv def fileload filename 程式設計客棧 待讀取.csv csvfile open filenamwww.cppcns.come,encodin...