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 with
block on block.
Comments
Post a Comment