2

I have 4 fields in the attribute table. After I merge these attribute fields in one single attribute field using FME Desktop 2020.1, it empty records within the fields are producing empty spaces and I want to get rid of empty spaces.

what I have:

Attribute field A

1,2,3,4
1, ,3,4
 ,2, ,4
 , , ,4
 , ,3, 
1,2, ,4


What I want:

Attribute field A

1,2,3,4
1,3,4
2,4
3
1,2,4
1
  • 1
    Please focus your question on the particular software you wish to ask about. You have a tag for ArcGIS Desktop and the only answer so far uses that software. You also mention FME Desktop and Python in your question body and title. I recommend you edit your question to focus it on ArcGIS Desktop so that the first answer is not stranded, and it can be re-opened. You can always ask about the other software in separate questions.
    – PolyGeo
    Commented Aug 9, 2020 at 0:22

1 Answer 1

3

It would be easier to merge the fields without the additional commas to begin with using a field calculation in ArcGIS Desktop.

  • Create "Field_A"

  • Open field calculator

  • Select Python parser

  • Tick "Show Codeblock"

  • in the "Pre-Logic Script Code" block define a function:

     def merge_fields(*fields):
         return ', '.join([f for f in fields if f])
    
  • in the "Field_A = " block:

     merge_fields(!FIELD_1!, !FIELD_2!, !FIELD_3!, !FIELD_4!)
    

Note: if your data is in shapefile format, ArcGIS uses spaces in text fields to indicate NULLs so you need to handle that by stripping them:

    def merge_fields(*fields):
        return ','.join([f.strip() for f in fields if f.strip()])
4
  • 2
    With FME use an AttributeTrimmer to remove any last ',' then a StringReplacer to replace ' ,' with nothing. Commented Aug 9, 2020 at 1:36
  • @user2856 thanks a lot it works just perfect, but how could I integrate these lines of code in PythonCaller in FME ?
    – gisgis
    Commented Aug 9, 2020 at 8:37
  • @gisgis I don't use FME. Do you want me to delete my answer?
    – user2856
    Commented Aug 9, 2020 at 8:59
  • 1
    no i found it helpful. It worked, but somehow my question it is closed and need to be reviewed for whatsoever reason that i dont understand. But again thanks a lot it worked in my case and I checked your answer as accepted answer.
    – gisgis
    Commented Aug 9, 2020 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.