d = { 'A': 85, 'B':70, 'C':40}
I need to write it as a .csv file with the title "title,score"
I tried two methods, but both of them do not work.
#Method 1:
f= open('score.csv','w')
f.write('Title,Score\n')
a = str(d)
for item in a.strip('{}').split(','):
item.replace(':',',')
f.write(item)
f.close()
#When I open score.csv, I do not know why it shows nothing.
#Method 2:
f= open('score.csv','w')
f.write('Title,Score\n')
with open('score.csv','w') as f:
for title in d.keys():
f.write('%s,%s\n'%(title,str(d[title]))
#method 2 cannot run, I guess it is because I cannot write the number in the file? But I tried str, it still cannot run.