python - How can I loop over a dictionary and write the name of the key as the name of the output file? -


i want loop on dictionary , make output file each key in dictionary , use key name of output file. tried:

for id, pos in pnposd.iteritems():   print id, 'id'   print pos, 'pos'   ofh = open("/home/",id,"_candmuts.txt")   ofh.write("%d\n" % (pos)) 

and error message got line try open input file (in line 4):

typeerror: file() takes @ 3 arguments (4 given) 

use str.format. should open file write mode (w) write file.

for id, pos in pnposd.iteritems():     print id, 'id'     print pos, 'pos'     open("/home/{}_candmuts.txt".format(id), 'w') ofh:         ofh.write("%d\n" % (pos)) 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -