我想导入包含4个值的database.csv
key,email1,email2,email3
filename,email@example.com,email2@example.com,email3@example.com
filename2,email@yahoo.com,email@google.com,email@outlook.com
etc,etc,etc,etc
接下来我想将列密钥分隔为等于文件名列表,将email1,email2和email3分隔为另一个列表
key = [filename]
emails = [email@example.com,email2@example.com,email3@example.com]
当前代码
import csv
with open('data.csv') as read_csv:
reader = csv.reader(read_csv)
for row in reader:
key = row[0]
emails = row[1::]
return key
return emails
输出
key = [filename2]
emails = [filename2,email@yahoo.com,email@google.com,email@outlook.com]
我需要的是与电子邮件相对应的密钥以传递给另一个函数。
答案 0 :(得分:1)
字典听起来像是适当的解决方案。
import csv
result = {}
with open('data.csv') as read_csv:
reader = csv.reader(read_csv)
for row in reader:
result[row[0]] = row[1:]
然后,您可以随时访问或传递如下值:
result[filename2]