Loop in a CSV

35 views
Skip to first unread message

A. Gonzalez

unread,
Aug 21, 2021, 6:29:06 AMAug 21
to Bots Open Source EDI Translator

Hello to all,

I have been "playing" with BOTS-EDI for some time now and so far I only use it to generate INVRPT. Something very simple.
Now I want to generate a DESADV from a pseudo-CSV. In the lines for the CPS PAC and LIN I can't get the loop to work properly.

These are the lines (CPS, PAC, and LIN) in the CSV.

400;1;0;;3;CT;;85.5;;;85.5;90123;20210809;
420;CT;01987654321184;1;;;
500;000001;1000;4012345678939;
500;000002;1000;4012345678938;
420;CT;01987654321185;2;;;
500;000005;1000;4012345678936;
400;1;0;;3;CT;;85.5;;;85.5;90123;20210809;
420;CT;01987654321184;1;;;
500;000001;1000;4012345678939;
500;000002;1000;4012345678938;
420;CT;01987654321185;2;;;
500;000005;1000;4012345678936;

This is my structure:

structure=    [
    {ID:'00',MIN:1,MAX:1,
        QUERIES:{
            'frompartner':  {'BOTSID':'000','EANZENDER':None},
            'topartner':    {'BOTSID':'000','EANONTVANGER':None},
            'reference':    {'BOTSID':'000','ORDERNUMMERAFNEMER':None},
            'testindicator':{'BOTSID':'000','TEST':None},
        },

        LEVEL:[
            {ID:'10',MIN:1,MAX:1,LEVEL:[
                {ID:'11',MIN:1,MAX:1},
                {ID:'12',MIN:1,MAX:1},
                {ID:'13',MIN:1,MAX:10},
                {ID:'14',MIN:1,MAX:1},
                {ID:'40',MIN:1,MAX:10,LEVEL:[
                    {ID:'50',MIN:1,MAX:10,LEVEL:[
                        {ID:'60',MIN:1,MAX:10}
                    ]},
                ]},
            ]},
        ]},
    ]  

And here ist my loop

    cpsline = 0
    for cps in inn.getloop({'BOTSID':'10'},{'BOTSID':'40'}):
        verzendregel = out.putloop({'BOTSID':'UNH'},{'BOTSID':'CPS'})
        cpsline = cpsline + 1
        verzendregel.put({'BOTSID':'CPS','7164':cpsline})
        verzendregel.put({'BOTSID':'CPS','7166':'111'})

        for pakk in cps.getloop({'BOTSID':'40'},{'BOTSID':'50'}):
            verpakking = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'PAC'})
            verpakking.put({'BOTSID':'PAC','7224':pakk.get({'BOTSID':'50','50_03':None})})
            verpakking.put({'BOTSID':'PAC','C202.7065':pakk.get({'BOTSID':'50','50_01':None})})
        for line in cps.getloop({'BOTSID':'400'},{'BOTSID':'420'},{'BOTSID':'500'}):
            regel = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'LIN'})
            regel.put({'BOTSID':'LIN','1082':line.get({'BOTSID':'500','500_01':None})})
            regel.put({'BOTSID':'LIN','C212.7143':'EN','C212.7140':line.get({'BOTSID':'500','500_03':None})})

    cpsline = 0
    for cps in inn.getloop({'BOTSID':'10'},{'BOTSID':'40'}):
        verzendregel = out.putloop({'BOTSID':'UNH'},{'BOTSID':'CPS'})
        cpsline = cpsline + 1
        verzendregel.put({'BOTSID':'CPS','7164':cpsline})
        verzendregel.put({'BOTSID':'CPS','7166':'111'})

        for pakk in cps.getloop({'BOTSID':'40'},{'BOTSID':'50'}):
            verpakking = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'PAC'})
            verpakking.put({'BOTSID':'PAC','7224':pakk.get({'BOTSID':'50','50_03':None})})
            verpakking.put({'BOTSID':'PAC','C202.7065':pakk.get({'BOTSID':'50','50_01':None})})
        for line in cps.getloop({'BOTSID':'40'},{'BOTSID':'50'},{'BOTSID':'60'}):
            regel = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'LIN'})
            regel.put({'BOTSID':'LIN','1082':line.get({'BOTSID':'60','60_01':None})})
            regel.put({'BOTSID':'LIN','C212.7143':'EN','C212.7140':line.get({'BOTSID':'60','60_03':None})})

My "mess" generates the following output:

CPS+1+111
PAC+1++CT
PAC+2++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
LIN+5++4012345678936:EN
CPS+2+111
PAC+1++CT
PAC+2++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
LIN+5++4012345678936:EN'

Expected was:

CPS+1+111
PAC+1++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
PAC+2++CT
LIN+5++4012345678936:EN
CPS+2+111
PAC+1++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
PAC+2++CT
LIN+5++4012345678936:EN


I'm like a monkey with guns with Python.... :P

I hope someone here can help me!
And thanks in advance for the support you give and the hours you dedicate to this group.

Best Regards,

Antonio

Eppye Bots

unread,
Aug 21, 2021, 6:38:52 AMAug 21
to 'Chuck Turco' via Bots Open Source EDI Translator
in the structure the 60 (lines) are nested under the 50 (verpakking)
in your looping not.

(confusing to read this; 40<->400 etc, seems you are doing much more than is in structure)

kind regards, Henk-Jan Ebbers


--
You received this message because you are subscribed to the Google Groups "Bots Open Source EDI Translator" group.
To unsubscribe from this group and stop receiving emails from it, send an email to botsmail+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/botsmail/1ee276dc-73fb-44ef-9a2b-f434804fc995n%40googlegroups.com.

A. Gonzalez

unread,
Aug 21, 2021, 7:09:28 AMAug 21
to Bots Open Source EDI Translator
Hi Henk-Jan,

thank you for your fast answer!
I made a mistake... now I send the corrected lines


These are the lines (CPS(40), PAC(50), and LIN(60)) in the CSV.

40;1;0;;3;CT;;85.5;;;85.5;90123;20210809;
50;CT;01987654321184;1;;;
60;000001;1000;4012345678939;
60;000002;1000;4012345678938;
50;CT;01987654321185;2;;;
60;000003;1000;4012345678936;
40;1;0;;3;CT;;85.5;;;85.5;90123;20210809;
50;CT;01987654321184;1;;;
60;000001;1000;4012345678939;
60;000002;1000;4012345678938;
50;CT;01987654321185;2;;;
60;000003;1000;4012345678936;




This is my structure:

structure=    [
    {ID:'00',MIN:1,MAX:1,
        QUERIES:{
            'frompartner':  {'BOTSID':'000','EANZENDER':None},
            'topartner':    {'BOTSID':'000','EANONTVANGER':None},
            'reference':    {'BOTSID':'000','ORDERNUMMERAFNEMER':None},
            'testindicator':{'BOTSID':'000','TEST':None},
        },

        LEVEL:[
            {ID:'10',MIN:1,MAX:1,LEVEL:[
                {ID:'11',MIN:1,MAX:1},
                {ID:'12',MIN:1,MAX:1},
                {ID:'13',MIN:1,MAX:10},
                {ID:'14',MIN:1,MAX:1},
                {ID:'40',MIN:1,MAX:10,LEVEL:[
                    {ID:'50',MIN:1,MAX:10,LEVEL:[
                        {ID:'60',MIN:1,MAX:10}
                    ]},
                ]},
            ]},
        ]},
    ]   



Here ist my loop


    cpsline = 0
    for cps in inn.getloop({'BOTSID':'10'},{'BOTSID':'40'}):
        verzendregel = out.putloop({'BOTSID':'UNH'},{'BOTSID':'CPS'})
        cpsline = cpsline + 1
        verzendregel.put({'BOTSID':'CPS','7164':cpsline})
        verzendregel.put({'BOTSID':'CPS','7166':'111'})

        for pakk in cps.getloop({'BOTSID':'40'},{'BOTSID':'50'}):
            verpakking = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'PAC'})
            verpakking.put({'BOTSID':'PAC','7224':pakk.get({'BOTSID':'50','50_03':None})})
            verpakking.put({'BOTSID':'PAC','C202.7065':pakk.get({'BOTSID':'50','50_01':None})})
        for line in cps.getloop({'BOTSID':'40'},{'BOTSID':'50'},{'BOTSID':'60'}):
            regel = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'LIN'})
            regel.put({'BOTSID':'LIN','1082':line.get({'BOTSID':'60','60_01':None})})
            regel.put({'BOTSID':'LIN','C212.7143':'EN','C212.7140':line.get({'BOTSID':'60','60_03':None})})


My "mess" generates the following output:

CPS+1+111
PAC+1++CT
PAC+2++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
LIN+3++4012345678936:EN

CPS+2+111
PAC+1++CT
PAC+2++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
LIN+3++4012345678936:EN'


Expected was:

CPS+1+111
PAC+1++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
PAC+2++CT
LIN+3++4012345678936:EN

CPS+2+111
PAC+1++CT
LIN+1++4012345678939:EN
LIN+2++4012345678938:EN
PAC+2++CT
LIN+3++4012345678936:EN

Thanks again for your Support!

Best Regards

Antonio

Eppye Bots

unread,
Aug 21, 2021, 7:22:21 AMAug 21
to 'Chuck Turco' via Bots Open Source EDI Translator
in the structure the 60 (lines) are nested under the 50 (verpakking)
in your looping not.

should look somewhat like this:
        for pakk in cps.getloop({'BOTSID':'40'},{'BOTSID':'50'}):
            verpakking = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'PAC'})
             for line in pakk.getloop({'BOTSID':'50'},{'BOTSID':'60'}):   #so this is nested 'under' pakk loop

                  regel = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'LIN'})



kind regards, Henk-Jan Ebbers


A. Gonzalez

unread,
Aug 21, 2021, 8:43:58 AMAug 21
to Bots Open Source EDI Translator
unfortunately I don't achieve my goal.
Now I take this output.

CPS+1+111
PAC+1++CT
PAC+2++CT
LIN+1++4013351470039:EN
LIN+2++4013351470039:EN
LIN+5++4013351470039:EN

Best Regards
Antonio

Eppye Bots

unread,
Aug 21, 2021, 8:52:35 AMAug 21
to 'Chuck Turco' via Bots Open Source EDI Translator
I would check out how looping works in python. as python depends on indenting, code can easily get garbled via mail/groups.
in order to avoid misunderstandings I attached file, maybe that helps.
else: to me it is clear where you make an error. it can be done right (the way you expect it).

kind regards, Henk-Jan Ebbers


test.txt

A. Gonzalez

unread,
Aug 31, 2021, 5:36:04 AMAug 31
to Bots Open Source EDI Translator
I tried yesterday but I get always the same,
LINs are outputed after PAC 1 and PAC2

I have tried nesting with one blank space like in your test.txt and I get a nested unexpected error.
With 4 blank spaces works but with the lines after PAC2
I think loops are working but they are not right nested. They go always to CPS and not to CPS-PAC.

Best Regards!
Antonio

Eppye Bots

unread,
Aug 31, 2021, 5:43:17 AMAug 31
to 'Chuck Turco' via Bots Open Source EDI Translator
looping works OK in bots, do not worry about that.
general rule for python: never use space to indent, always use tabs.

what is current mapping?
did you solve this issue:
>> in the structure the 60 (lines) are nested under the 50 (verpakking)
>> in your looping not.



kind regards, Henk-Jan Ebbers


A. Gonzalez

unread,
Aug 31, 2021, 5:36:54 PMAug 31
to Bots Open Source EDI Translator
Although I like tabs better, I have always believed that spaces were better for python.

I switched to tabs and inspiration struck!
Now it is working as I expected.

This is the mapping that is working:

    cpsline = 1
    for cps in inn.getloop({'BOTSID':'10'},{'BOTSID':'40'},{'BOTSID':'50'}):

        verzendregel = out.putloop({'BOTSID':'UNH'},{'BOTSID':'CPS'})
        cpsline += 1

        verzendregel.put({'BOTSID':'CPS','7164':cpsline})
        verzendregel.put({'BOTSID':'CPS','7166':'1'})
        pakkcounter = 0     
        pakkcounter += 1
        verzendregel.put({'BOTSID':'CPS'},{'BOTSID':'PAC','7224':pakkcounter})
        verzendregel.put({'BOTSID':'CPS'},{'BOTSID':'PAC','C202.7065':cps.get({'BOTSID':'50','50_01':None})})
        verzendregel.put({'BOTSID':'CPS'},{'BOTSID':'PAC'},{'BOTSID':'PCI','4233':'IEN','C210.7102#1':cps.get({'BOTSID':'50','50_02':None})})
        linecounter=0
        for line in cps.getloop({'BOTSID':'50'},{'BOTSID':'60'}):

            regel = verzendregel.putloop({'BOTSID':'CPS'},{'BOTSID':'LIN'})
            linecounter += 1
            regel.put({'BOTSID':'LIN','1082':linecounter})

            regel.put({'BOTSID':'LIN','C212.7143':'EN','C212.7140':line.get({'BOTSID':'60','60_03':None})})
            regel.put({'BOTSID':'LIN'},{'BOTSID':'PIA','4347':'1','C212#1.7143':'SA','C212#1.7140':line.get({'BOTSID':'60','60_04':None})})
            regel.put({'BOTSID':'LIN'},{'BOTSID':'PIA','4347':'1','C212#1.7143':'IN','C212#1.7140':line.get({'BOTSID':'60','60_04':None})})
            regel.put({'BOTSID':'LIN'},{'BOTSID':'IMD','7077':'F','C273.7008#1':line.get({'BOTSID':'60','60_07':None})})
            regel.put({'BOTSID':'LIN'},{'BOTSID':'QTY','C186.6063':'12','C186.6060':line.get({'BOTSID':'60','60_11':None})})
            regel.put({'BOTSID':'LIN'},{'BOTSID':'QTY','C186.6063':'12','C186.6411':'PCE'})     #:line.get({'BOTSID':'60','60_21':None})})


There are some data that are fakes because I have used fields in use to test it. Now I still have to do the correct
mapping of the CSV fields, but the most important thing is already done!

This time it took me a while to find the solution!

Best Regards!
Antonio

Eppye Bots

unread,
Aug 31, 2021, 6:01:06 PMAug 31
to 'Chuck Turco' via Bots Open Source EDI Translator
the whole CPS/PAC/LIN thing is always complicated.
but you succeeded! cool!
kind regards, Henk-Jan Ebbers


A. Gonzalez

unread,
Sep 1, 2021, 11:32:34 AMSep 1
to Bots Open Source EDI Translator
Thank you Henk-Jan!

When you see Bots doing what you need, it's wonderful. It also motivates you to continue with other new projects.
I hope I won't have to bother you too much in the group!

Best Regards
Antonio
Reply all
Reply to author
Forward
0 new messages