我已经苦恼了一段时间,希望有人能帮忙。 我有一个字典列表。每个字典都应该写入一个单独的.csv文件。我该怎么做?
目前我所拥有的代码并不能实现:
from openpyxl import *
from openpyxl.styles import *
import csv
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
one = {'A':a,'B':b}
two = {'C':c,'D':d}
three = {'E':e,'F':f}
four = {'G':g,'H':h}
list = [one,two,three,four]
def main():
for eachfilename, eachdict in enumerate(list):
with open(eachfilename, 'w') as csvfile:
writer = csv.writer(csvfile)
for line in eachdict:
writer.writerow(line)
main()
最终我希望有四个看起来像这样的.csv文件:
文件1:
A,a
B,b
文件2:
C,c
D,d
File3: ...
for
循环后添加一个简单的print eachfilename,eachdict
,你是否能看到任何有意义的东西(即文件名和字典,这是你的后续代码所假设的)? - DisappointedByUnaccountableMod