0

Here is the code I am using:

import csv
import os

filename = "studentinfo.csv"
filedir = ""
csv_file = os.path.join(filedir, filename)

names = []
ages = []
emails = []

with open(csv_file) as file:
    reader = csv.DictReader(file)
    for row in reader:
        names.append(row.get('name'))
        ages.append(row.get('age'))
        emails.append(row.get('email'))

print(names, ages, emails)

and this is the CSV file:

name, age, email
fin harris, 14,email1
harvey green, 14, email2

I am getting this output:

['fin harris', 'harvey green'] [None, None] [None, None]

Why am I getting the output None?

1
  • Remove all the whitespaces from both the header line and the data lines Commented Nov 7, 2019 at 12:01

1 Answer 1

1

It is because of whitespaces before column name. You can use row.get(' age') or remove whitespaces from column names.

Sign up to request clarification or add additional context in comments.

1 Comment

The better solution is to remove the whitespaces. If not for the csv """standards""" then for the consistency of the column names. Whitespaces should also be removed from the data rows

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.