[英]Python: Zipping multiple lists into a dictionary
Ok, so I have 6 lists taken from a csv file that I want to create a dictionary from, with one list being the key value and the other 5 each containing a value attached to that key. 好的,所以我从要创建词典的csv文件中提取了6个列表,其中一个列表是键值,其他5个都包含附加到该键的值。 I them am deleting certain rows if their keys match a value in a 7th list (c6).
如果它们的键与第7个列表(c6)中的值匹配,我将删除某些行。 Any help is appreciated, write now I'm stuck getting the lists to populate from the correct column of the csv.
感谢您的帮助,请立即写信,让我从csv的正确列中填充列表。 I also have a messy amount of readers since they can only be looped through once.
我也有很多读者,因为它们只能循环浏览一次。
with open (r'filea', 'r', newline='') as f:
tread=csv.reader(f, delimiter=',')
aread=csv.reader(f, delimiter=',')
bread=csv.reader(f, delimiter=',')
cread=csv.reader(f, delimiter=',')
dread=csv.reader(f, delimiter=',')
eread=csv.reader(f, delimiter=',')
tlist= []
alist= []
blist= []
clist= []
dlist= []
elist= []
for row in tread:
tlist.append(row[0])
for row in aread:
alist.append(row[1])
print (alist)
for row in bread:
blist.append(row[2])
print (blist)
for row in cread:
clist.append(row[3])
for row in dread:
dlist.append(row[4])
for row in eread:
elist.append(row[5])
fulldict = dict(zip(tlist, [alist, blist, clist, dlist, elist]))
print (fulldict)
for key in c6:
if key in fulldict:
del fulldict[key]
if key not in fulldict:
pass
with open (r'fileb', 'w', newline='') as g:
wrt=csv.writer(g)
for row in fulldict.items():
wrt.writerow(row)
with open (r'filea', 'r', newline='') as f:
data = {}
exclude = set()
for row in csv.reader(f, delimiter=','):
data[row[0]] = row[1:6]
exclude.add(row[6])
for key in exclude:
if key in data:
del data[key]
with open (r'fileb', 'w', newline='') as g:
wrt=csv.writer(g)
# do the writing
I ended with do the writing
because I don't know what you want to write. 我结束
do the writing
因为我不知道你想写什么。 Your writing code is wrong, as items()
of dict is a list of (key, value)
tuples. 您的编写代码是错误的,因为dict的
items()
是(key, value)
元组的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.