Python - Write Header Once - Loop isn't working properly -


i trying write "order file" header , detail lines. getting order header write file, 1 detail line seems written file.

for k, v in atlantic_billing.iteritems():     xhorno = str(digits + counter)     xhcsno = k     print xhcsno     machines = v     line = 1     counter = counter + 1     header_written = false     try :         machine in machines :             xdorno = xhorno             xdorsq = line             line = line + 1             xditd1 = ranpak_dict[machine]['model']             xditd2 = ranpak_dict[machine]['sn']             xdcavc = ranpak_dict[machine]['total']             print xdorsq, xditd1, xditd2, xdcavc             if xdcavc > 0 :                 if header_written == false :                     open(xhorno + ".txt", 'w') order:                         order.write("h01, " + xhorno + ", " + xhcsno + "\n")                         order.write("d01," + str(xdorsq) + ", " + ' eqpranuser, ' + xditd1 + ", " +  xditd2 + ", " + xdcavc + "\n")                 else :                     order.write("d01," + str(xdorsq) + ", " + ' eqpranuser, ' + xditd1 + ", " +  xditd2 + ", " + xdcavc + "\n")                     success.append(machine)                     header_written = true      except keyerror, e:         issues.append(machine) 

when opening file, should use mode "a" else file overwritten @ each loop (again , again):

 open(xhorno + ".txt", 'a') order:      ... 

see https://docs.python.org/3/library/functions.html#open

an other option take withblock on block.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -