After importing a csv into python as a list object, how do i refer to one of the columns?

3592 views python-3.x

I imported a csv into python3 as a List object. Assuming that the headers of the original csv were "FirstName", "LastName", "EmailAddress", "MailAddress", "PhoneNumber", how would I list only the rows that have no string in the "EmailAddress" column? Or how would I list only the rows that have no entry under the "PhoneNumber" column. Im not sure how to refer to specific rows in the List.

If the name of the List was called "ContactInfo", is there a way to specify just the PhoneNumber column? In powershell it would look like....


answered question

1 Answer


A good way to figure this out would just be to print the list that you have.

Typically, csv to list returns a list of lists, where each inner element list is one row. You can refer individual elements of this inner list element (specific columns) using indices

So you can do :

For row in list_from_csv :

    If row[2] = ...... (condition)
    If row[4] = ...... (condition)

Here we are checking the 3rd and 5th column in each row.

posted this

Have an answer?


Please login first before posting an answer.