我有一个txt文件,其中包含以下格式的列表:
Shoes, Nike, Addias, Puma,...other brand names
Pants, Dockers, Levis,...other brand names
Watches, Timex, Tiesto,...other brand names
如何按照以下格式将它们放入字典中: dictionary={Shoes: [Nike, Addias, Puma,.....] Pants: [Dockers, Levis.....] Watches:[Timex, Tiesto,.....] }
如何使用for循环而不是手动输入。
我已经尝试过。
clothes=open('clothes.txt').readlines()
clothing=[]
stuff=[]
for line in clothes:
items=line.replace("\n","").split(',')
clothing.append(items[0])
stuff.append(items[1:])
Clothing:{}
for d in clothing:
Clothing[d]= [f for f in stuff]
line.rstrip()
)。否则,答案很好。(line.rstrip().split(', ') for line in open('clothes.txt').readlines())
? - Antimony