Python: Read CSV into a list of lists or tuples or dictionaries
文章推薦指數: 80 %
We opened the csv file in read mode and then passed the file object to csv.DictReader() function. It returned an iterator dict_reader, with which we can iterate ... Skiptocontent InthisarticlewewilldiscusshowtoimportaCSVintolist.Thislistcanbealistoflists,listoftuplesorlistofdictionaries.WewillalsousepandasmoduleandcoverscenariosforimportingCSVcontentstolistwithorwithoutheaders. SupposewehaveaCSVfile‘students.csv’anditscontentsare, Id,Name,Course,City,Session 21,Mark,Python,London,Morning 22,John,Python,Tokyo,Evening 23,Sam,Python,Paris,Morning 32,Shaun,Java,Tokyo,Morning Nowlet’sseehowtoimportthecontentsofthiscsvfileintoalist. ReadaCSVintolistoflistsinpython Therearedifferentwaystoloadcsvcontentstoalistoflists, Importcsvtoalistoflistsusingcsv.reader Pythonhasabuilt-incsvmodule,whichprovidesareaderclasstoreadthecontentsofacsvfile.Let’susethat, fromcsvimportreader #readcsvfileasalistoflists withopen('students.csv','r')asread_obj: #passthefileobjecttoreader()togetthereaderobject csv_reader=reader(read_obj) #Passreaderobjecttolist()togetalistoflists list_of_rows=list(csv_reader) print(list_of_rows) Output: [['Id','Name','Course','City','Session'], ['21','Mark','Python','London','Morning'], ['22','John','Python','Tokyo','Evening'], ['23','Sam','Python','Paris','Morning'], ['32','Shaun','Java','Tokyo','Morning']] Itcreatedalistoflistscontainingallrowsofcsvfileandprintthatlistoflists. Advertisements Howdiditwork? Weopenedthefileinreadmodeandthenpassedthefileobjecttocsv.reader()function.Itreturnedaniterator,whichcanbeusedtoiterateoverallthelinesofcsvfile.Butwepassedthisiteratorobjecttolist()function,whichreturnalistoflistsi.e.whereeachlistrepresentsarowofcsvandeachiteminthelistrepresentsacell/columninthatrow. Selectspecificvalueincsvbyspecificrowandcolumnnumber Wecanalsoselectanindividualelementincsvfilebyrow&columnnumberusingthislistoflistscreatedabove.Forexample,fromthelistoflistsi.e.list_of_rows(createdabove),let’sselectthevaluefromcsvatrownumber3andcolumnnumber2, #selectthevaluefromcsvatrownumber3andcolumnnumber2 row_number=3 col_number=2 value=list_of_rows[row_number-1][col_number-1] print('Valueincellat3rdrowand2ndcolumn:',value) Output: Valueincellat3rdrowand2ndcolumn:John UsePandastoreadcsvintoalistoflistswithoutheader Inthepreviousexample,weloadedallrows(includingheader)intoalistoflists.Supposewewanttoreadallrowsintoalistoflistsexceptheader.Wecanachievethateasilyusingpandas, importpandasaspd #Createadataframefromcsv df=pd.read_csv('students.csv',delimiter=',') #UserlistcomprehensiontocreatealistoflistsfromDataframerows list_of_rows=[list(row)forrowindf.values] #Printlistoflistsi.e.rows print(list_of_rows) Output: [['21','Mark','Python','London','Morning'], ['22','John','Python','Tokyo','Evening'], ['23','Sam','Python','Paris','Morning'], ['32','Shaun','Java','Tokyo','Morning']] Itcreatedalistoflistscontainingallrowsofcsvexceptheaderandprintthatlistoflists. Howdiditwork? WeloadedthecsvtoaDataframeusingread_csv()function.AsDataframe.valuesreturnsa2DNumpyrepresentationofallrowsofDataframeexcludingheader.So,weiteratedoverallrowsofthis2DNumpyArrayusinglistcomprehensionandcreatedalistoflists.Whereeachlistrepresentsarowofcsvandeachiteminthelistrepresentsacell/columninthatrow. UsePandastoreadcsvintoalistoflistswithheader Inaboveexample,headerofcsvwasskippedbydefault.So,ifyouwantheadertoointhislistoflists, thenweneedtoinsertitinlistseparatelyintheendoftheaboveexample,likethis, importpandasaspd #Createadataframefromcsv df=pd.read_csv('students.csv',delimiter=',') #UserlistcomprehensiontocreatealistoflistsfromDataframerows list_of_rows=[list(row)forrowindf.values] #InsertColumnnamesasfirstlistinlistoflists list_of_rows.insert(0,df.columns.to_list()) #Printlistoflistsi.e.rows print(list_of_rows) Output: [['Id','Name','Course','City','Session'], ['21','Mark','Python','London','Morning'], ['22','John','Python','Tokyo','Evening'], ['23','Sam','Python','Paris','Morning'], ['32','Shaun','Java','Tokyo','Morning']] Itcreatedalistoflistscontainingallrowsofcsvincludingheaderandprintthatlistoflists. Howdiditwork? WeloadedthecsvtoaDataframeusingread_csv()function.AsDataframe.valuesreturnsa2DnumpyrepresentationofallrowsofDataframeexcludingheader.So,weiteratedoverallrowsofthis2DNumpyArrayusinglistcomprehensionandcreatedalistoflists.Dataframe.columnsgivescolumnnames,soweconvertedthattoalistandinsertedatthestartoftherowslist. ReadcsvintolistoftuplesusingPython Let’sloadallthecontentsofstudents.csvtoalistoftuples,whereeachtupleinthelistrepresentsarowandeachvalueinthetuplerepresentsacell/columnvalueforthatparticularrow. fromcsvimportreader #openfileinreadmode withopen('students.csv','r')asread_obj: #passthefileobjecttoreader()togetthereaderobject csv_reader=reader(read_obj) #Getallrowsofcsvfromcsv_readerobjectaslistoftuples list_of_tuples=list(map(tuple,csv_reader)) #displayallrowsofcsv print(list_of_tuples) Output [('Id','Name','Course','City','Session'), ('21','Mark','Python','London','Morning'), ('22','John','Python','Tokyo','Evening'), ('23','Sam','Python','Paris','Morning'), ('32','Shaun','Java','Tokyo','Morning')] Howdiditwork? Weopenedthecsvfileinreadmodeandthenpassedthefileobjecttocsv.reader()function.Itreturnedaniteratorcsv_reader,withwhichwecaniterateoveralltherowsofcsv.Butwepasseditintothemap()functionasanargumentalongwithtuple()functionascallbacki.e., mapped_object=map(tuple,csv_reader) map()functionacceptsafunction&inputlistasarguments.Foreachitemintheinputlistitappliesthefunctionandstorestheresultinamappedobject. So,inthiscasemap()function,iteratedoveralltherowsofcsvusingiteratorcsv_readerandappliedthefunctiontuple()toeachitem.Thenstoredthereturnedtuplei.e.atupleforarow,tothemappedobject.So,nowourmapped_objectcontainstuples,i.e.onetupleforeachrow. Thenweconvertedthismappedobjectoftuplestoalistoftuplesi.e. list_of_tuples=list(mapped_object) So,thisishowweimportedacsvfiletoalistoftuples.Wecandothesameusingpandas&listcomprehensioninasinglelinei.e. Readcsvintolistoftuplesusingpandas&listcomprehension importpandasaspd #Createadataframefromcsv df=pd.read_csv('students.csv',delimiter=',') #CreatealistoftuplesforDataframerowsusinglistcomprehension list_of_tuples=[tuple(row)forrowindf.values] #Printlistoftuple print(list_of_tuples) Output [('21','Mark','Python','London','Morning'), ('22','John','Python','Tokyo','Evening'), ('23','Sam','Python','Paris','Morning'), ('32','Shaun','Java','Tokyo','Morning')] WeloadedthecsvtoaDataframeusingread_csv()function.Dataframe.valuesreturnedaNumpyrepresentationoftheDataFramei.e.isa2Dnumpyarray.Usinglistcomprehensionweiteratedovereachrowofthis2Darrayandforeachrowweconvertedthattoatuple,thenappendittoalist. Finallylistcomprehensionreturnedalistoftuples,whereeachtupleinthelistrepresentsarowandeachvalueinthetuplerepresentsacell/columnvalueforthatparticularrow. Readcsvintolistofdictionariesusingpython fromcsvimportDictReader #openfileinreadmode withopen('students.csv','r')asread_obj: #passthefileobjecttoDictReader()togettheDictReaderobject dict_reader=DictReader(read_obj) #getalistofdictionariesfromdct_reader list_of_dict=list(dict_reader) #printlistofdicti.e.rows print(list_of_dict) Output: [{'Id':'21','Name':'Mark','Course':'Python','City':'London','Session':'Morning'}, {'Id':'22','Name':'John','Course':'Python','City':'Tokyo','Session':'Evening'}, {'Id':'23','Name':'Sam','Course':'Python','City':'Paris','Session':'Morning'}, {'Id':'32','Name':'Shaun','Course':'Java','City':'Tokyo','Session':'Morning'}] Wegotalistofdictionaries,whereeachdictionaryinthelistrepresentsacsvrowandcontainspairsofcolumnnames&columnvaluesforthatrow,askey/valuepairs. Howdiditwork? Weopenedthecsvfileinreadmodeandthenpassedthefileobjecttocsv.DictReader()function.Itreturnedaniteratordict_reader,withwhichwecaniterateoveralltherowsofcsvandfetcheachrowcontentasadictionary.Butwepassedthisiteratortothelist()function,whichreturnedalistofdictionariesi.e.rows. Animportantpointis,wheneverwepassaniterableitemtoalistconstructori.e.list(),thenitinternallyiteratesoveralltheelementsofthedatastructuretowhichthisiteratorobjectispointing &storesthemintoalist.Intheendreturnsthatlist. Thecompleteexampleisasfollows, fromcsvimportreader fromcsvimportDictReader importpandasaspd defmain(): print('****Readcsvintoalistoflists****') #readcsvfileasalistoflists withopen('students.csv','r')asread_obj: #passthefileobjecttoreader()togetthereaderobject csv_reader=reader(read_obj) #Passreaderobjecttolist()togetalistoflists list_of_rows=list(csv_reader) print(list_of_rows) print('***SelectvalueincsvSpecificRowandColumn***') #selectthevaluefromcsvatrownumber3andcolumnnumber2 row_number=3 col_number=2 value=list_of_rows[row_number-1][col_number-1] print('Valueincellat3rdrowand2ndcolumn:',value) print('***Usepandastoreadrowsinacsvfiletoalistoflistwithoutheader***') #Createadataframefromcsv df=pd.read_csv('students.csv',delimiter=',') #UserlistcomprehensiontocreatealistoflistsfromDataframerows list_of_rows=[list(row)forrowindf.values] #Printlistoflistsi.e.rows print(list_of_rows) print('***Usepandastoreadrowsinacsvfiletoalistoflist***') #Createadataframefromcsv df=pd.read_csv('students.csv',delimiter=',') #UserlistcomprehensiontocreatealistoflistsfromDataframerows list_of_rows=[list(row)forrowindf.values] #InsertColumnnamesasfirstlistinlistoflists list_of_rows.insert(0,df.columns.to_list()) #Printlistoflistsi.e.rows print(list_of_rows) print('****ReadcsvintolistoftuplesusingPython****') #openfileinreadmode withopen('students.csv','r')asread_obj: #passthefileobjecttoreader()togetthereaderobject csv_reader=reader(read_obj) #Getallrowsofcsvfromcsv_readerobjectaslistoftuples list_of_tuples=list(map(tuple,csv_reader)) #displayallrowsofcsv print(list_of_tuples) print('***Readcsvintolistoftuplesusingpandasinpython(withoutheader)***') #Createadataframefromcsv df=pd.read_csv('students.csv',delimiter=',') #CreatealistoftuplesforDataframerowsusinglistcomprehension list_of_tuples=[tuple(row)forrowindf.values] #Printlistoftuple print(list_of_tuples) print('****Readcsvintolistofdictionariesusingpython****') #openfileinreadmode withopen('students.csv','r')asread_obj: #passthefileobjecttoDictReader()togettheDictReaderobject dict_reader=DictReader(read_obj) #getalistofdictionariesfromdct_reader list_of_dict=list(dict_reader) #printlistofdicti.e.rows print(list_of_dict) if__name__=='__main__': main() Output: ****Readcsvintoalistoflists**** [['Id','Name','Course','City','Session'],['21','Mark','Python','London','Morning'],['22','John','Python','Tokyo','Evening'],['23','Sam','Python','Paris','Morning'],['32','Shaun','Java','Tokyo','Morning']] ***SelectvalueincsvSpecificRowandColumn*** Valueincellat3rdrowand2ndcolumn:John ***Usepandastoreadrowsinacsvfiletoalistoflistwithoutheader*** [[21,'Mark','Python','London','Morning'],[22,'John','Python','Tokyo','Evening'],[23,'Sam','Python','Paris','Morning'],[32,'Shaun','Java','Tokyo','Morning']] ***Usepandastoreadrowsinacsvfiletoalistoflist*** [['Id','Name','Course','City','Session'],[21,'Mark','Python','London','Morning'],[22,'John','Python','Tokyo','Evening'],[23,'Sam','Python','Paris','Morning'],[32,'Shaun','Java','Tokyo','Morning']] ****ReadcsvintolistoftuplesusingPython**** [('Id','Name','Course','City','Session'),('21','Mark','Python','London','Morning'),('22','John','Python','Tokyo','Evening'),('23','Sam','Python','Paris','Morning'),('32','Shaun','Java','Tokyo','Morning')] ***Readcsvintolistoftuplesusingpandasinpython(withoutheader)*** [(21,'Mark','Python','London','Morning'),(22,'John','Python','Tokyo','Evening'),(23,'Sam','Python','Paris','Morning'),(32,'Shaun','Java','Tokyo','Morning')] ****Readcsvintolistofdictionariesusingpython**** [{'Id':'21','Name':'Mark','Course':'Python','City':'London','Session':'Morning'},{'Id':'22','Name':'John','Course':'Python','City':'Tokyo','Session':'Evening'},{'Id':'23','Name':'Sam','Course':'Python','City':'Paris','Session':'Morning'},{'Id':'32','Name':'Shaun','Course':'Java','City':'Tokyo','Session':'Morning'}] PandasTutorials-LearnDataAnalysiswithPython PandasTutorialPart#1-IntroductiontoDataAnalysiswithPython PandasTutorialPart#2-BasicsofPandasSeries PandasTutorialPart#3-Get&SetSeriesvalues PandasTutorialPart#4-Attributes&methodsofPandasSeries PandasTutorialPart#5-AddorRemovePandasSerieselements PandasTutorialPart#6-IntroductiontoDataFrame PandasTutorialPart#7-DataFrame.loc[]-SelectRows/ColumnsbyIndexing PandasTutorialPart#8-DataFrame.iloc[]-SelectRows/ColumnsbyLabelNames PandasTutorialPart#9-FilterDataFrameRows PandasTutorialPart#10-Add/RemoveDataFrameRows&Columns PandasTutorialPart#11-DataFrameattributes&methods PandasTutorialPart#12-HandlingMissingDataorNaNvalues PandasTutorialPart#13-IterateoverRows&ColumnsofDataFrame PandasTutorialPart#14-SortingDataFramebyRowsorColumns PandasTutorialPart#15-MergingorConcatenatingDataFrames PandasTutorialPart#16-DataFrameGroupByexplainedwithexamples AreyoulookingtomakeacareerinDataSciencewithPython? DataScienceisthefuture,andthefutureisherenow.DataScientistsarenowthemostsought-afterprofessionalstoday.TobecomeagoodDataScientistortomakeacareerswitchinDataScienceonemustpossesstherightskillset.WehavecuratedalistofBestProfessionalCertificateinDataSciencewithPython.ThesecourseswillteachyoutheprogrammingtoolsforDataSciencelikePandas,NumPy,Matplotlib,SeabornandhowtousetheselibrariestoimplementMachinelearningmodels. CheckouttheDetailedReviewofBestProfessionalCertificateinDataSciencewithPython. Remember,DataSciencerequiresalotofpatience,persistence,andpractice.So,startlearningtoday. JoinaLinkedInCommunityofPythonDevelopers Postnavigation ←PreviousPostNextPost→ RelatedPosts LeaveaCommentCancelReplyYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Typehere..Name* Email* Website Δ ThissiteusesAkismettoreducespam.Learnhowyourcommentdataisprocessed. Advertisements Advertisements RecentPosts ReplaceastringinmultiplefilesinLinux RecursivelyCountFilesinadirectoryinLinux FindFilescontainingspecificTextinLinux FindlatestmodifiedfilesinadirectoryinLinux(Recursively) DownloadafilefromaserverusingSSH PrintadirectorystructurelikeatreeinLinux ChangepermissionsforDirectory&Sub-directoriesinLinux HowtonormalizecolumnsinPandasDataFrame? HowtoGettheindexcolumnnameinPandas? HowtoResetIndexinaPandasDataFrame? PythonTutorialsLookingforSomething Searchfor: Search Manageyourprivacy Toprovidethebestexperiences,weandourpartnersusetechnologieslikecookiestostoreand/oraccessdeviceinformation.ConsentingtothesetechnologieswillallowusandourpartnerstoprocesspersonaldatasuchasbrowsingbehaviororuniqueIDsonthissite.Notconsentingorwithdrawingconsent,mayadverselyaffectcertainfeaturesandfunctions.Clickbelowtoconsenttotheaboveormakegranularchoices. Yourchoiceswillbeappliedtothissiteonly. Youcanchangeyoursettingsatanytime,includingwithdrawingyourconsent,byusingthetogglesontheCookiePolicy,orbyclickingonthemanageconsentbuttonatthebottomofthescreen. Functional Functional Alwaysactive Thetechnicalstorageoraccessisstrictlynecessaryforthelegitimatepurposeofenablingtheuseofaspecificserviceexplicitlyrequestedbythesubscriberoruser,orforthesolepurposeofcarryingoutthetransmissionofacommunicationoveranelectroniccommunicationsnetwork. Preferences Preferences Thetechnicalstorageoraccessisnecessaryforthelegitimatepurposeofstoringpreferencesthatarenotrequestedbythesubscriberoruser. Statistics Statistics Thetechnicalstorageoraccessthatisusedexclusivelyforstatisticalpurposes. Thetechnicalstorageoraccessthatisusedexclusivelyforanonymousstatisticalpurposes.Withoutasubpoena,voluntarycomplianceonthepartofyourInternetServiceProvider,oradditionalrecordsfromathirdparty,informationstoredorretrievedforthispurposealonecannotusuallybeusedtoidentifyyou. Marketing Marketing Thetechnicalstorageoraccessisrequiredtocreateuserprofilestosendadvertising,ortotracktheuseronawebsiteoracrossseveralwebsitesforsimilarmarketingpurposes. Statistics Marketing Features Alwaysactive Alwaysactive Manageoptions Manageservices Managevendors Readmoreaboutthesepurposes Accept Deny Manageoptions Savepreferences Manageoptions {title} {title} {title} Manageyourprivacy Toprovidethebestexperiences,weusetechnologieslikecookiestostoreand/oraccessdeviceinformation.ConsentingtothesetechnologieswillallowustoprocessdatasuchasbrowsingbehaviororuniqueIDsonthissite.Notconsentingorwithdrawingconsent,mayadverselyaffectcertainfeaturesandfunctions. Functional Functional Alwaysactive Thetechnicalstorageoraccessisstrictlynecessaryforthelegitimatepurposeofenablingtheuseofaspecificserviceexplicitlyrequestedbythesubscriberoruser,orforthesolepurposeofcarryingoutthetransmissionofacommunicationoveranelectroniccommunicationsnetwork. Preferences Preferences Thetechnicalstorageoraccessisnecessaryforthelegitimatepurposeofstoringpreferencesthatarenotrequestedbythesubscriberoruser. Statistics Statistics Thetechnicalstorageoraccessthatisusedexclusivelyforstatisticalpurposes. Thetechnicalstorageoraccessthatisusedexclusivelyforanonymousstatisticalpurposes.Withoutasubpoena,voluntarycomplianceonthepartofyourInternetServiceProvider,oradditionalrecordsfromathirdparty,informationstoredorretrievedforthispurposealonecannotusuallybeusedtoidentifyyou. Marketing Marketing Thetechnicalstorageoraccessisrequiredtocreateuserprofilestosendadvertising,ortotracktheuseronawebsiteoracrossseveralwebsitesforsimilarmarketingpurposes. Manageoptions Manageservices Managevendors Readmoreaboutthesepurposes Accept Deny Manageoptions Savepreferences Manageoptions {title} {title} {title} Manageconsent Manageconsent ScrolltoTop
延伸文章資訊
- 1How to Read a CSV File Into a List in Python - LearnPython.com
- 2Convert List of Lists to CSV File in Python - PythonForBeginners.com
- 3How To Read CSV to List in Python - Studytonight
Example: Reading CSV to List in Python · It created a list of lists containing all rows of the CS...
- 4Python – Convert CSV to List of Lists - Finxter
To read a CSV to a nested list in pure Python, open the file using open('my_file.csv') , read all...
- 5Read CSV to List in Python | Delft Stack
Python has a built-in module named CSV , which has a reader class to read the contents of a CSV f...