AlliDeacon February 2016

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

I am trying to write an "order file" with a Header and Detail Lines. I am successfully getting the order Header to write to file, but only one detail line seems to get written to the 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 :
        for 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 :
                    with open(XHORNO + ".txt", 'w') as 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)

Answers


Antoine February 2016

When opening the file, you should use the mode "A" else the file will be overwritten at each loop (again and again):

 with open(XHORNO + ".txt", 'a') as order:
     ...

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

An other option is to take the withblock over the for block.


Yoav Glazner February 2016

You need to open the file only once and not inside the loop

for k, v in atlantic_billing.iteritems():
    XHORNO = str(digits + counter)
    with open(XHORNO + ".txt", 'w') as order: # <--- here you go
        XHCSNO = k
        print XHCSNO
        machines = v
        line = 1
        counter = counter + 1
        header_written = False
        try :
            for 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 :

                        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)


David Cullen February 2016

Try this:

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 :
        for 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 :
                with open(XHORNO + ".txt", 'a') as order:
                    if header_written == False :
                        order.write("H01, " + XHORNO + ", " + XHCSNO + "\n")
                        order.write("D01," + str(XDORSQ) + ", " + ' EQPRANUSER, ' + XDITD1 + ", " +  XDITD2 + ", " + XDCAVC + "\n")
                        header_written = True
                    else :
                        order.write("D01," + str(XDORSQ) + ", " + ' EQPRANUSER, ' + XDITD1 + ", " +  XDITD2 + ", " + XDCAVC + "\n")
                        success.append(machine)

    except KeyError, e:
        issues.append(machine)

Post Status

Asked in February 2016
Viewed 1,828 times
Voted 7
Answered 3 times

Search




Leave an answer