LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Test if field exists (and if not, output the field with Nulls)

    Employee
    Posted 12-10-2013 05:59

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: SambaDancer

    Dear all,

    In the "XMLpy File" node, there is this nice trick where you can test if a field exists in the input XML.
    - if so, output the field
    - if not, create the field anyway and fill with NULL

    An example of the trick would look something like this:

    @elementHandler('/auditfile/transactions')
    def transactionsHandler(element):
    data = {}

    data['fieldName'] = None
    if hasattr(element, "fieldName"):
    data['fieldName'] = element.fieldName


    WOULD YOU KNOW IF THERE'S A SIMILAR TRICK ON NON-XML FIELDS???

    The reason I ask is because I'm making a script based on source data that can vary slightly every time. For example: sometimes all address information is stored in 1 field "Address" and sometimes it is split accross multiple fields "Streetname", "HouseNumber", "PostalCode", etc. Upfront, I cannot predict which method is used, both are allowed.

    Of course, I want the remainder of my script to be completely generic, so "somewhere" the fields need to exist, whether filled with NULLs or not...

    Any ideas, anyone?

    Greetings and thanks in advance from The Netherlands,

    Bart...


  • 2.  RE: Test if field exists (and if not, output the field with Nulls)

    Employee
    Posted 12-13-2013 13:42

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: SambaDancer

    Dear all,
    Okay, I've solved this one myself (got a brainwave while drive home ;-)
    What you have to do is so simple, I just didn't think it up at first:
    - just insert a Dummy Input node, NumberOfRecords = 0 and fill the Header with all prescribed fields (comma-separated) in the desired order.
    - then insert a Cat node, ConcatenationNode = union.
    - attach the output from the Dummy Input as the first input for the Cat node (because then your 'force' the output in your desired order :-)
    - attach the output from the original data as the second input for the Cat node (drag it on top of the Cat node, so a 2nd input is created).
    - Run, Forrest, Run!
    Now, all fields from your original data will just 'pass through', whereas omitted fields will be added from the Dummy Input, filled with NULL. Exactly what I wanted!
    The only thing you want to check, of course, is that the fields from the original data have exactly the same name as the corresponding ones from the Dummy Input (duhhh).
    Hope this helps your, might you ever come accross the same problem.
    Best regards,
    Bart (Coney B.V., The Netherlands).