Data360 Analyze

 View Only
  • 1.  Removing spaces from string in column

    Posted 03-27-2023 09:25

    The data source I'm connecting to is always pulling back a 5 character string, however, the actual data is not always 5 characters. It could be 3, 4, or 5. The data with less than 5 characters is padding with spaces to bring it to 5. Below is the script I created, however, it's not replacing the spaces, and I'm currently accounting for only 1 space character. It's also not kicking back any errors. Is there an easier to remove the spaces and\or what's wrong with my script?

        out1 += in1
    if ' ' in fields['S&E']:
        str1 = fields['S&E']
        str2 = str1.replace(' ', '')
        fields['S&E'] = str2
    else:
        pass



    ------------------------------
    Shawn Pelletier
    Knowledge Community Shared Account
    ------------------------------


  • 2.  RE: Removing spaces from string in column

    Posted 03-28-2023 03:05

    Something like this would be easier to comment on if you include a couple records of sample data.

    I think you might want to change the line fields['S&E'] = str2 to be out1['S&E'] = str2

    If you don't expect an array then I probably wouldn't personally use "in". I don't think its doing what you expect here.

    I think you actually want something like this:  (check for minor typos etc)

    if in1['S&E']: 

    out1['S&E']=in1['S&E'].replace(' ','')

    The "if" is is only needed if some data is not defined, you might need stronger null checking. Also replace will actually remove multiple occurrences

    If you really do have a list then the first tweak should work.



    ------------------------------
    Peter Sykes
    Vontobel Holding AG
    ZUERICH
    ------------------------------



  • 3.  RE: Removing spaces from string in column

    Employee
    Posted 03-28-2023 09:31

    Rather than creating custom code to perform this task, you could use the Trim node.

    Given the following test data in a Create Data node:

    You can configure the Trim node to trim the space characters from the required field as follows:

    In this case the node is configured to only process the 'Data' field but by default it will remove leading and trailing space characters around values in all string/unicode fields.



    ------------------------------
    Adrian Williams
    Precisely Software Inc.
    ------------------------------



  • 4.  RE: Removing spaces from string in column

    Employee
    Posted 03-28-2023 09:40

    If you wanted to use a Transform node with some custom code then I would still recommend using the standard Python functions to perform the trimming of the string values. Note you will have to explicitly handle the case where the input data is Null if this can happen with your source data.



    ------------------------------
    Adrian Williams
    Precisely Software Inc.
    ------------------------------