Fin Harris
Fin Harris

Reputation: 45

Getting 'None' Output when reading from CSV file

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?

Upvotes: 0

Views: 369

Answers (1)

Nurullah Macun
Nurullah Macun

Reputation: 552

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

Upvotes: 1

Related Questions