如何在python中从csv中提取列

2024-06-16 14:58:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习如何使用Python中的csv,我想阅读csv的第3列并打印出内容。我正在使用下面的代码,但运行时得到的输出为0

    with open('prospects.csv', 'r') as csv_file:
        pros = csv.reader(csv_file)
        for row in pros:
            print(row[2])

以下是CSV文件的一个片段:

rank,team,age,teamId,full_name,playerId,atBats,runs,hits,doubles,triples,homeRuns,obp,ops,slg,rbi,baseOnBalls,strikeOuts,stolenBases,caughtStealing,leftOnBase,totalBases,avg,position,sportAbbrev
1,tb,19,,Wander Franco,677551,425,82,139,27,7,9,.398,.885,.487,53,56,35,18,14,177,207,.327,SS,ALL (2)
2,la,22,,Gavin Lux,666158,533,111,177,29,9,28,.405,.983,.578,85,68,126,12,6,178,308,.332,SS,ALL (3)
3,cws,22,,Luis Robert,673357,503,108,165,31,11,32,.376,1.001,.624,92,28,129,36,11,186,314,.328,CF,ALL (3)
4,bal,22,,Adley Rutschman,668939,130,19,33,8,1,4,.351,.774,.423,26,20,27,1,0,56,55,.254,C,ALL (3)
6,ana,21,,Jo Adell,666176,350,63,100,31,0,11,.356,.825,.469,38,34,106,8,0,151,164,.286,OF,ALL (4)

Tags: csv代码in内容foraswithopen