0

I am learning about working with csv's in Python and I would like to read the 3rd column of a csv and print out the contents. I am using the code below but getting an output of 0 when I run it.

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

Below is a snippet of the CSV file:

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)
4
  • using your exact code I seem to be getting the ages. Is there something else missing? Commented Apr 16, 2020 at 15:57
  • Yeah, works for me too. I only had to add an import csv to the top and it works fine. Commented Apr 16, 2020 at 16:00
  • Generally speaking: data = [row[column] for row in reader] Commented Apr 16, 2020 at 16:46
  • Weird that it doesn't work in pycharm for me even though syntax is correct Commented Apr 16, 2020 at 17:05

1 Answer 1

0

if you are open to using pandas (Pandas), then

import pandas as pd
data = pd.read_csv('prospects.csv')

to get the 'age' column use

data['age']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.