pang2016 pang2016 - 9 days ago 5
Python Question

csv file name match with dict keys

I encounter a problem:

data={'a':'1-2-3','b':'3-4-5','c':'','d':'3--3'}
for i in data.values():
df=pandas.DataFrame(i.split('-'))
df.to_csv('a.csv')


I want get four csv file:

a.csv
0
0 1
1 2
2 3

b.csv
0
0 3
1 4
2 5

c.csv

d.csv
0
0 3
1
2 3


I try use format and other meathod, but it can't work.Thanks a lot

Answer

I think need loop by items():

for k,v in data.items():
    df = pandas.DataFrame(v.split('-'))
    df.to_csv(k + '.csv')
Comments