1

I have a csv file with 3 columns. I'd like to read the file and put column number 2 into a list.

I currently have it reading the file and putting all three columns as tuples into a list:

    import csv


    with open ('list.csv', 'rb') as f:
           reader = csv.reader(f)
           the_list = map(tuple, reader)


    print the_list

output = [('1', 'bob', '23'), ('2', 'jane', '21')]

whereas I want the output list to be [('bob'), ('jane')] and I'm uncertain of how to do it. Thanks

2 Answers 2

1

Use list comprehension.

reader = csv.reader(f)
print [i[1] for i in reader]
Sign up to request clarification or add additional context in comments.

Comments

0
import csv

with open('jjon.csv', 'rU') as infile:

    reader = csv.reader(infile)
    print [i[0] for i in reader]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.