Python import csv to list - Stack Overflow

文章推薦指數: 80 %
投票人數:10人

Using the csv module: import csv with open('file.csv', newline='') as f: reader = csv.reader(f) data = list(reader) print(data). Output: Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams Pythonimportcsvtolist AskQuestion Asked 8years,3monthsago Modified 5monthsago Viewed 786ktimes 246 IhaveaCSVfilewithabout2000records. Eachrecordhasastring,andacategorytoit: Thisisthefirstline,Line1 Thisisthesecondline,Line2 Thisisthethirdline,Line3 Ineedtoreadthisfileintoalistthatlookslikethis: data=[('Thisisthefirstline','Line1'), ('Thisisthesecondline','Line2'), ('Thisisthethirdline','Line3')] HowcanimportthisCSVtothelistIneedusingPython? pythoncsv Share Improvethisquestion Follow editedFeb15,2020at6:07 AMC 2,58477goldbadges1212silverbadges3434bronzebadges askedJul9,2014at19:48 MorganTNMorganTN 2,62733goldbadges1212silverbadges1010bronzebadges 3 2 Thenusecsvmodule:docs.python.org/2/library/csv.html – furas Jul9,2014at19:53 6 Ifthereisananswerthatsuitsyourquestion,pleaseacceptit. – MaciejGol Mar24,2015at21:37 2 PossibleduplicateofHowdoIreadandwriteCSVfileswithPython? – MartinThoma Jan11,2017at7:47 Addacomment  |  13Answers 13 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 422 Usingthecsvmodule: importcsv withopen('file.csv',newline='')asf: reader=csv.reader(f) data=list(reader) print(data) Output: [['Thisisthefirstline','Line1'],['Thisisthesecondline','Line2'],['Thisisthethirdline','Line3']] Ifyouneedtuples: importcsv withopen('file.csv',newline='')asf: reader=csv.reader(f) data=[tuple(row)forrowinreader] print(data) Output: [('Thisisthefirstline','Line1'),('Thisisthesecondline','Line2'),('Thisisthethirdline','Line3')] OldPython2answer,alsousingthecsvmodule: importcsv withopen('file.csv','rb')asf: reader=csv.reader(f) your_list=list(reader) printyour_list #[['Thisisthefirstline','Line1'], #['Thisisthesecondline','Line2'], #['Thisisthethirdline','Line3']] Share Improvethisanswer Follow editedFeb15,2020at6:42 AMC 2,58477goldbadges1212silverbadges3434bronzebadges answeredJul9,2014at19:55 MaciejGolMaciejGol 14.9k44goldbadges3232silverbadges5050bronzebadges 11 4 Whydoyouuse'rb'insteadof'r'? – imrek May21,2015at14:28 6 @DrunkenMaster,bcausesthefiletobeopenedinbinarymodeasopposedtotextmode.Onsomesystemstextmodemeansthat\nwillbeconvertestoplatform-specificnewlinewhenreadingorwriting.Seedocs. – MaciejGol May24,2015at8:12 7 ThisdoesnotworkinPython3.x:"csv.Error:iteratorshouldreturnstrings,notbytes(didyouopenthefileintextmode?)"SeebelowfortheanswerthatworksinPython3.x – Gilbert May30,2016at18:12 3 tosaveafewsecondsoftimedebugging,youshouldprobablyaddanoteforthefirstsolution,like"Python2.xversion" – paradite Jan30,2017at9:03 Howtouseyour1stsolutionbutwithonlysomecolumnsfromthecsvfile? – Sigur May6,2017at3:13  |  Show6morecomments 67 UpdatedforPython3: importcsv withopen('file.csv',newline='')asf: reader=csv.reader(f) your_list=list(reader) print(your_list) Output: [['Thisisthefirstline','Line1'],['Thisisthesecondline','Line2'],['Thisisthethirdline','Line3']] Share Improvethisanswer Follow editedJan9,2020at19:46 AMC 2,58477goldbadges1212silverbadges3434bronzebadges answeredFeb11,2016at13:43 seokhoonleeseokhoonlee 9781111silverbadges1717bronzebadges 1 Specifying'r'isthedefaultmode,sospecifyingitisunnecessary.ThedocsalsomentionIfcsvfileisafileobject,itshouldbeopenedwithnewline=''. – AMC Jan6,2020at1:40 Addacomment  |  54 Pandasisprettygoodatdealingwithdata.Hereisoneexamplehowtouseit: importpandasaspd #ReadtheCSVintoapandasdataframe(df) #Withadfyoucandomanythings #mostimportant:visualizedatawithSeaborn df=pd.read_csv('filename.csv',delimiter=',') #Orexportitinmanyways,e.g.alistoftuples tuples=[tuple(x)forxindf.values] #orexportitasalistofdicts dicts=df.to_dict().values() Onebigadvantageisthatpandasdealsautomaticallywithheaderrows. Ifyouhaven'theardofSeaborn,Irecommendhavingalookatit. Seealso:HowdoIreadandwriteCSVfileswithPython? Pandas#2 importpandasaspd #Getdata-readingtheCSVfile importmpu.pd df=mpu.pd.example_df() #Convert dicts=df.to_dict('records') Thecontentofdfis: countrypopulationpopulation_timeEUR 0Germany82521653.02016-12-01True 1France66991000.02017-01-01True 2Indonesia255461700.02017-01-01False 3Ireland4761865.0NaTTrue 4Spain46549045.02017-06-01True 5VaticanNaNNaTTrue Thecontentofdictsis [{'country':'Germany','population':82521653.0,'population_time':Timestamp('2016-12-0100:00:00'),'EUR':True}, {'country':'France','population':66991000.0,'population_time':Timestamp('2017-01-0100:00:00'),'EUR':True}, {'country':'Indonesia','population':255461700.0,'population_time':Timestamp('2017-01-0100:00:00'),'EUR':False}, {'country':'Ireland','population':4761865.0,'population_time':NaT,'EUR':True}, {'country':'Spain','population':46549045.0,'population_time':Timestamp('2017-06-0100:00:00'),'EUR':True}, {'country':'Vatican','population':nan,'population_time':NaT,'EUR':True}] Pandas#3 importpandasaspd #Getdata-readingtheCSVfile importmpu.pd df=mpu.pd.example_df() #Convert lists=[[row[col]forcolindf.columns]forrowindf.to_dict('records')] Thecontentoflistsis: [['Germany',82521653.0,Timestamp('2016-12-0100:00:00'),True], ['France',66991000.0,Timestamp('2017-01-0100:00:00'),True], ['Indonesia',255461700.0,Timestamp('2017-01-0100:00:00'),False], ['Ireland',4761865.0,NaT,True], ['Spain',46549045.0,Timestamp('2017-06-0100:00:00'),True], ['Vatican',nan,NaT,True]] Share Improvethisanswer Follow editedJan6,2020at10:01 answeredNov24,2016at13:52 MartinThomaMartinThoma 113k148148goldbadges572572silverbadges877877bronzebadges 1 tuples=[tuple(x)forxindf.values]canbewrittentuples=list(df.itertuples(index=False))instead.DonotethatthePandasdocsdiscouragetheuseof.valuesinfavourof.to_numpy().Thethirdexampleisconfusingtome.First,becausethevariableisnamedtuples,whichwouldimplythatitisalistoftuples,whereasit'sactuallyalistoflists.Second,becauseasfarasIcantellthatentireexpressioncanbereplacedwithdf.to_list().Ialsodon'tknowifthesecondexampleisreallyrelevanthere. – AMC Jan6,2020at2:05 Addacomment  |  11 UpdateforPython3: importcsv frompprintimportpprint withopen('text.csv',newline='')asfile: reader=csv.reader(file) res=list(map(tuple,reader)) pprint(res) Output: [('Thisisthefirstline','Line1'), ('Thisisthesecondline','Line2'), ('Thisisthethirdline','Line3')] Ifcsvfileisafileobject,itshouldbeopenedwithnewline=''. csvmodule Share Improvethisanswer Follow editedJan6,2020at19:11 AMC 2,58477goldbadges1212silverbadges3434bronzebadges answeredJan5,2018at3:12 AbstProcDoAbstProcDo 18.6k1414goldbadges7171silverbadges123123bronzebadges 1 Whyuselist(map())overalistcomprehension?Also,noticethewhitespaceinatthebeginningofeachelementofthesecondcolumn. – AMC Jan6,2020at17:14 Addacomment  |  5 Ifyouaresuretherearenocommasinyourinput,otherthantoseparatethecategory,youcanreadthefilelinebylineandspliton,,thenpushtheresulttoList Thatsaid,itlookslikeyouarelookingataCSVfile,soyoumightconsiderusingthemodulesforit Share Improvethisanswer Follow editedMay23,2017at12:02 CommunityBot 111silverbadge answeredJul9,2014at19:53 MiquelMiquel 15.2k88goldbadges5353silverbadges8686bronzebadges Addacomment  |  4 result=[] forlineintext.splitlines(): result.append(tuple(line.split(","))) Share Improvethisanswer Follow answeredJul9,2014at19:54 Acid_SnakeAcid_Snake 5922bronzebadges 4 2 Canyoupleaseaddabitofexplanationtothispost?Codeonlyis(sometimes)good,butcodeandexplanationis(mosttimes)better – Barranka Jul9,2014at20:29 3 IknowBarranka'scommentisoverayearold,butforanyonewhostumblesuponthisandcan'tfigureitout:forlineintext.splitlines():putseachindividuallineintempvariable"line".line.split(",")createsalistofstringsthataresplitonthecomma.tuple(~)putsthatlistinatupleandappend(~)addsittotheresult.Aftertheloop,resultisalistoftuples,witheachtuplealine,andeachtupleelementanelementinthecsvfile. – Louis Oct18,2015at10:05 Inadditiontowhat@Louissaid,thereisnoneedtouse.read().splitlines(),youcaniterateovertheeachlineofthefiledirectly:forlineinin_file:res.append(tuple(line.rstrip().split(",")))Also,donotethatusing.split(',')meansthateveryelementofthesecondcolumnwillbeginwithextrawhitespace. – AMC Jan6,2020at17:22 AddendumtothecodeIjustsharedabove:line.rstrip()->line.rstrip('\n'). – AMC Jan6,2020at17:29 Addacomment  |  4 Youcanusethelist()functiontoconvertcsvreaderobjecttolist importcsv withopen('input.csv',newline='')ascsv_file: reader=csv.reader(csv_file,delimiter=',') rows=list(reader) print(rows) Share Improvethisanswer Follow editedMay1at16:39 PedroContipelli 14666bronzebadges answeredAug23,2020at3:01 KiddoKiddo 1,14711goldbadge1111silverbadges2323bronzebadges Addacomment  |  3 Asimpleloopwouldsuffice: lines=[] withopen('test.txt','r')asf: forlineinf.readlines(): l,name=line.strip().split(',') lines.append((l,name)) printlines Share Improvethisanswer Follow answeredJul9,2014at19:54 HunterMcMillenHunterMcMillen 57.9k2222goldbadges116116silverbadges165165bronzebadges 2 1 Whatifsomeoftheentrieshavecommasinthem? – TonyEnnis Feb16,2016at17:59 @TonyEnnisThenyouwouldneedtouseamoreadvancedprocessingloop.TheanswerbyMaciejaboveshowshowtousethecsvparserthatcomeswithPythontoperformthisoperation.Thisparsermostlikelyhasallofthelogicyouneed. – HunterMcMillen Feb16,2016at18:21 Addacomment  |  3 Assaidalreadyinthecommentsyoucanusethecsvlibraryinpython.csvmeanscommaseparatedvalueswhichseemsexactlyyourcase:alabelandavalueseparatedbyacomma. BeingacategoryandvaluetypeIwouldratheruseadictionarytypeinsteadofalistoftuples. AnywayinthecodebelowIshowbothways:disthedictionaryandlisthelistoftuples. importcsv file_name="test.txt" try: csvfile=open(file_name,'rt') except: print("Filenotfound") csvReader=csv.reader(csvfile,delimiter=",") d=dict() l=list() forrowincsvReader: d[row[1]]=row[0] l.append((row[0],row[1])) print(d) print(l) Share Improvethisanswer Follow editedDec4,2018at15:21 answeredJun11,2018at8:26 FrancescoBoiFrancescoBoi 7,5141212goldbadges6969silverbadges109109bronzebadges 5 Whynotuseacontextmanagertohandlethefile?Whyareyoumixingtwodifferentvariablenamingconventions?Isn't(row[0],row[1])weaker/moreerror-pronethanjustusingtuple(row)? – AMC Jan6,2020at1:22 Whydouthinkdoingtuple(row)islesserrorprone?whatvariablenamingconventionareureferringto?Pleaselinkanofficialpythonnamingconvention.AsfarasIknowtry-exceptisagoodwaytohandlefiles:whatdoumeanbycontexthandler? – FrancescoBoi Jan6,2020at15:41 Whydouthinkdoingtuple(row)islesserrorprone?Becauseitdoesn’trequirethatyouwriteouteverysingleindexmanually.Ifyoumakeamistake,orthenumberofelementschanges,youhavetogobackandchangeyourcode.Thetry-exceptisfine,contextmanagersarethewithstatement.Youcanfindplentyofresourcesonthesubject,suchasthisone. – AMC Jan6,2020at15:44 Idon'tseehowthecontextmanagerwouldbebetterthantheol'goodtry-exceptblock.Fortheotherthepositiveaspectisthatutypelesscode;fortherestifnumberofelements(Iguessumeanthenumberofcolumns)changesmineisbetterbecauseitisextractingonlythedesiredvalueswhiletheotherit'sextractingalltheexcel.Withoutanyspecificrequirementyoucannotsaywhichisbettersoit'sawasteoftimearguingwhichisbetter:inthiscasebotharevalid – FrancescoBoi Jan6,2020at16:09 Idon'tseehowthecontextmanagerwouldbebetterthantheol'goodtry-exceptblock.Pleaseseemypreviouscomment,thecontextmanagerwouldnotreplacethetry-except. – AMC Jan6,2020at17:07 Addacomment  |  1 UnfortunatelyIfindnoneoftheexistinganswersparticularlysatisfying. HereisastraightforwardandcompletePython3solution,usingthecsvmodule. importcsv withopen('../resources/temp_in.csv',newline='')asf: reader=csv.reader(f,skipinitialspace=True) rows=list(reader) print(rows) Noticetheskipinitialspace=Trueargument.Thisisnecessarysince,unfortunately,OP'sCSVcontainswhitespaceaftereachcomma. Output: [['Thisisthefirstline','Line1'],['Thisisthesecondline','Line2'],['Thisisthethirdline','Line3']] Share Improvethisanswer Follow editedJan9,2020at17:57 answeredJan6,2020at1:18 AMCAMC 2,58477goldbadges1212silverbadges3434bronzebadges Addacomment  |  0 Extendingyourrequirementsabitandassumingyoudonotcareabouttheorderoflinesandwanttogetthemgroupedundercategories,thefollowingsolutionmayworkforyou: >>>fname="lines.txt" >>>fromcollectionsimportdefaultdict >>>dct=defaultdict(list) >>>withopen(fname)asf: ...forlineinf: ...text,cat=line.rstrip("\n").split(",",1) ...dct[cat].append(text) ... >>>dct defaultdict(,{'CatA':['Thisisthefirstline','Thisistheanotherline'],'CatC':['Thisisthethirdline'],'CatB':['Thisisthesecondline','Thisisthelastline']}) Thiswayyougetallrelevantlinesavailableinthedictionaryunderkeybeingthecategory. Share Improvethisanswer Follow editedApr3,2018at21:40 Marvin 22911goldbadge33silverbadges1111bronzebadges answeredJul9,2014at20:08 JanVlcinskyJanVlcinsky 41.4k1212goldbadges9999silverbadges9797bronzebadges Addacomment  |  0 HereistheeasiestwayinPython3.xtoimportaCSVtoamultidimensionalarray,anditsonly4linesofcodewithoutimportinganything! #pullaCSVintoamultidimensionalarrayin4lines! L=[]#Createanemptylistforthemainarray forlineinopen('log.txt'):#Openthefileandreadallthelines x=line.rstrip()#Stripthe\nfromeachline L.append(x.split(','))#Spliteachlineintoalistandaddittothe #Multidimensionalarray print(L) Share Improvethisanswer Follow answeredAug30,2019at21:38 JasonBoucherJasonBoucher 911bronzebadge 1 1 Becareful,it'salist,notanarray!Whynotuseacontextmanagertoproperlyhandlethefileobject?Notethatthissolutionleavesextrawhitespaceontheseconditemineachrow,andthatitwillfailifanyofthedatacontainsacomma. – AMC Jan6,2020at1:19 Addacomment  |  -1 Nextisapieceofcodewhichusescsvmodulebutextractsfile.csvcontentstoalistofdictsusingthefirstlinewhichisaheaderofcsvtable importcsv defcsv2dicts(filename): withopen(filename,'rb')asf: reader=csv.reader(f) lines=list(reader) iflen(lines)<2:returnNone names=lines[0] iflen(names)<1:returnNone dicts=[] forvaluesinlines[1:]: iflen(values)!=len(names):returnNone d={} fori,_inenumerate(names): d[names[i]]=values[i] dicts.append(d) returndicts returnNone if__name__=='__main__': your_list=csv2dicts('file.csv') printyour_list Share Improvethisanswer Follow answeredJul12,2017at8:06 AlexeyAntonenkoAlexeyAntonenko 2,2531616silverbadges1717bronzebadges 1 1 Whynotjustusecsv.DictReader? – AMC Jan6,2020at0:10 Addacomment  |  Highlyactivequestion.Earn10reputation(notcountingtheassociationbonus)inordertoanswerthisquestion.Thereputationrequirementhelpsprotectthisquestionfromspamandnon-answeractivity. Nottheansweryou'relookingfor?Browseotherquestionstaggedpythoncsvoraskyourownquestion. TheOverflowBlog HowtoearnamillionreputationonStackOverflow:beofservicetoothers Therightwaytojobhop(Ep.495) FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... CollectivesUpdate:RecognizedMembers,Articles,andGitLab Shouldweburninatethe[script]tag? Linked -3 ReadcsvfileintolistoflistswithoutstringconvertioninPython -4 Readingamultipleline.txtfileintoanarrayPython -5 SplitCSVfileusingpython 0 Howtosplitpartsoflistelementsseparatedbycommasintonewlists? -2 PythonSplitlines/wordsfrominputfilesintovariables 1 splittextfilewithregularexpressionpython 1 Howtocallonavaluewithinalist -1 Isthereaneasywaytoaccessanelementinasublistusingcsvreader? -1 HowdoInotstripoutthecommasin"1,234"whenIiteratethroughafileinpython -5 Needhelponpythoncoding Seemorelinkedquestions Related 3230 HowdoIcheckifalistisempty? 6975 WhataremetaclassesinPython? 4153 Findingtheindexofaniteminalist 7492 DoesPythonhaveaternaryconditionaloperator? 4855 HowdoImakeaflatlistoutofalistoflists? 3467 HowdoIlistallfilesofadirectory? 2025 Howtoreadafileline-by-lineintoalist? 3588 DoesPythonhaveastring'contains'substringmethod? 1271 Howtoreadatextfileintoastringvariableandstripnewlines? 2112 WhyisreadinglinesfromstdinmuchslowerinC++thanPython? HotNetworkQuestions Myfavoriteanimalisa-singularandpluralform CounterexampleforChvatal'sconjectureinaninfiniteset Whyistherealotofcurrentvariationattheoutputofabuckwhenabatteryisconnectedattheoutput? Howdoyoucalculatethetimeuntilthesteady-stateofadrug? Areyougettingtiredofregularcrosswords? HowtogetridofUbuntuProadvertisementwhenupdatingapt? Whydoes«facture»mean"bill,invoice"? Whatdoyoucallastatementthatisgivenasanexplanationwhysomeonehaswonanaward? HowtofindthebordercrossingtimeofatraininEurope?(Czechbureaucracyedition) Whatistheconventionalwaytonotateameterwithaccentsoneverysecond8thnote? WhydopeopleinsistonusingTikzwhentheycanusesimplerdrawingtools? Realitycheck:PolarCO2lakescoexistingwithanequatorialH2Oocean Sciencefictionbook/novelaboutaliensinhumans'bodies Wouldextractinghydrogenfromthesunlessenitslifespan? Canaphotonturnaprotonintoaneutron? Traditionally,andcurrently,whatstopshumanvotecountersfromalteringballotstomakethem'Spoilt/Invalidvotes? ReturnoftheJedi-"northtower"-howcanyoutellthecardinalpointsincenterofasphere? 2016PutnamB6difficultsummationproblem IfthedrowshadowbladeusesShadowSwordasarangedattack,doesitthrowasword(thatitthenhastoretrievebeforeusingitagain)? Whydoesthesameelectrontransitionreleasephotonsofdifferentfrequenciesforsomeelements? InD&D3.5,canafamiliarbetemporarilydismissed? Canananimalfilealawsuitonitsownbehalf? Whyismyropeweird-looking? Howtotellifmybikehasanaluminumframe morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?