數(shù)據(jù)分析過程(準(zhǔn)備2)

csv format
csv comma seperated values
like a spreadsheet with no formulas
easy to process with code

python中的csv

在python中,csv文件通常呈現(xiàn)為一個由行組成的列表
1.each row is a list
the overall data structure is a list of lists

csv=[['A1','A2','A3'],
     ['B1','B2','B3']]

2.csv文件有標(biāo)題行
each row is a dictionary
the keys of each dictionary can be column names and the fields can be values
the overall data structure is a list of dictionary

csv=[{'name1':'A1','name2':'A2','name3':'A3'},
     {'name1':'B1','name2':'B2','name3':'B3'}]

unicode讀取csv

import unicodecsv
enrollments=[]
f=open('enrollments.csv')
reader = unicodecsv.DictReader(f)   #reader 并不是行列表,而是迭代器,可用迭代器編寫獲取各元素的循環(huán),但這是一次性的

for row in reader :
    enrollments.append(row)

for row in reader:           #如果用第二個循環(huán)打印出reader中的所有行,那么輸出結(jié)果為空,因為對每個迭代器只能進(jìn)行一次循環(huán)
    print row 
f.close()
enrollments[0]

優(yōu)化版:

import unicodecsv
with open('enrollments.csv','rb') as f:     #使用with語句避免最后還要關(guān)閉文件
    reader = unicodecsv.DictReader(f)
    enrollments = list(reader)              #將迭代器轉(zhuǎn)化為列表
enrollments[0]

讀取
enrollments.csv
daily_engagement.csv
project_submissions.csv
三個文件的數(shù)據(jù),并打印第一行

import unicodecsv
def read_csv(filename):
    with open(filename,'rb') as f:
        reader = unicodecsv.DictReader(f)
        return list(reader)
enrollments = read_csv('enrollments.csv')
daily_engagement = read_csv('daily-engagement.csv')
project_submissions = read_csv('project-submissions.csv')
print enrollments[0]
print daily_engagement[0]
print project_submissions[0]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容