Python import csv to list - Stack Overflow
文章推薦指數: 80 %
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(
延伸文章資訊
- 1Python: Read CSV into a list of lists or tuples or dictionaries
We opened the csv file in read mode and then passed the file object to csv.DictReader() function....
- 2How to Import CSV to List Python - Linux Hint
Line 1: We import the CSV module. Line 2 to 4: We open the sample.csv file in the read mode 'r'. ...
- 3Reading Rows from a CSV File in Python - GeeksforGeeks
- 4How to read specific column from CSV file in Python - Adam Smith
- 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...