从CSV fi读取时获取“无”输出

2022-07-06 11:37:50 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我正在使用的代码:

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)

这是CSV文件:

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

我得到以下输出:

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

为什么我没有得到输出


Tags: csvnameimportnonegetnamesosfilenamereaderfilerowemailsappendfiledirages