使用python獲取csv文字的某行或某列資料

2021-08-28 00:16:49 字數 662 閱讀 8612

csv是comma-separated values的縮寫,是用文字檔案形式儲存的**資料,比如如下的**:

要提取其中某一列,可以用下面的**:

import csv

with open('a.csv','rb') as csvfile:

reader = csv.reader(csvfile)

column = [row[2] for row in reader]

print column得到:['age', '12', '13', '14', '15']

如果我們想用dictreader讀取csv的某一列,就可以用列的標題查詢:

import csv

with open('a.csv','rb') as csvfile:

reader = csv.dictreader(csvfile)

column = [row['age'] for row in reader]

print column

就得到:

['12', '13', '14', '15']

使用python獲取csv文字的某行或某列資料

站長用python寫了乙個可以提取csv任一列的 歡迎使用。github鏈結 要提取其中某一列,可以用下面的 import csv with open a.csv rb as csvfile reader csv.reader csvfile column row 2 for row in read...

python使用csv寫入csv檔案

沒什麼好說的,直接上 吧 with open file.csv w encoding utf 8 newline as csvfile writer csv.writer csvfile 首先是表頭 writer.writerow id name gender birthday rating 然後是...

python使用csv讀寫CSV檔案

檔案的讀 import csv with open test.csv r as csv file reader csv.reader csv file for line in reader print line 檔案的寫 import csv 表頭 file header houseinfo hou...