您正在寻找DictReader
with open('info.csv') as f: reader = csv.DictReader(f, delimiter=';') for row in reader: name = row['name'] blah = row['blah']
引用链接:
创建一个像常规阅读器一样操作的对象,但将读取的信息映射到一个dict,其键由可选的fieldnames参数给出....如果省略fieldnames参数,则csvfile第一行中的值将用作字段名.
您可以使用csv.DictReader实例来获取此行为。
来自文档的示例:
>>> with open('names.csv') as csvfile: ... reader = csv.DictReader(csvfile) ... for row in reader: ... print(row['first_name'], row['last_name']) ... Eric Idle John Cleese