如何从CSV文件检查列标题在Python中是否有效

时间:2019-05-21 06:49:47

标签: python python-3.x csv validation

我有一个CSV文件,其中包含具有以下列标题的数据:first_namelast_nameemail。我想确认CSV文件中的列必须按上面显示的顺序排序。我已经实现了代码,但问题是获取列标题。我坚持要验证此顺序的原因是,我正在创建具有firstnamelastnameemail作为字段的患者对象,并且如果CSV文件已重新排序,则数据错误最终将被添加到字段中。

if file_serializer.is_valid():
    data = self.request.data.get('file')
    data_set = data.read().decode('UTF-8')
    io_string = io.StringIO(data_set)
    io_string = io.StringIO(data_set)
    next(io_string)

    for column in csv.reader(io_string, delimiter=',', quotechar="|"):
        # Add validation here
        print(column)
        obj, created = Patient.objects.get_or_create(
                firstname=column[0],
                lastname=column[1],
                email=column[2]
            )

CSV屏幕截图

enter image description here

示例文本

first_name,last_name,email
Shaylynn,Klimentyev,sklimentyev0@cisco.com
Thaddeus,Grushin,tgrushin1@umn.edu
Hayden,Cranmer,hcranmer2@nationalgeographic.com
Colet,Ferfulle,cferfulle3@unesco.org
Ernestine,Hardi,ehardi4@virginia.edu
Freddi,Knagges,fknagges5@ifeng.com

2 个答案:

答案 0 :(得分:2)

列是否具有正确的顺序并不重要,只要它们具有正确的列标题即可。您可以使用DictReader处理这些标题:

for row in csv.DictReader(io_string, delimiter=',', quotechar='|'):
    print(row['first_name'], row['last_name'], row['email'])

如果您真的坚持要验证订单,也可以这样做:

list(row.keys()) == ['first_name', 'last_name', 'email']

答案 1 :(得分:0)

myiter = csv.reader(io_string, delimiter=',', quotechar="|")
header = next(myiter)
print("HEADER:", header)
for column in myiter:
    # Add validation here
    print(column)