简体   繁体   English

Python - 写入标题一次 - 循环不能正常工作

[英]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)

When opening the file, you should use the mode "A" else the file will be overwritten at each loop (again and again): 打开文件时,应使用"A"模式,否则文件将在每个循环中被覆盖(一次又一次):

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

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

An other option is to take the with block over the for block. 另一种选择是在for块上with块。

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM