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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -