这是我尝试过的:
def CreateDict(BDF):
with open('%sdata\\%s.csv' % (mainDir,BDF), mode='r') as infile:
reader = csv.reader(infile)
for row in reader:
key = row[0]
print("%s%s = %s" % (BDF,[key],row[1:]))
CreateDict(FileName)
打印给了我正确的答案。但是我想把它放在字典中,而不是只打印出来。我希望该字典的名称为FileName。
答案 0 :(得分:1)
要创建字典:
def CreateDict(BDF):
saved={}
with open('%sdata\\%s.csv' % (mainDir,BDF), mode='r') as infile:
reader = csv.reader(infile)
for row in reader:
key = row[0]
saved[key]=row[1:]
return saved
我不知道如何给dict一个特定的名字,如果你需要保存多个CSV文件,你可以使用一个字典,其中的键是fileNames:
fileNames=['test0', 'test1', 'test2']
all_files={}
for fileName in fileNames:
all_files[fileName]=CreateDict(fileName)