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
-
1Please 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– PolyGeo ♦2020年08月09日 00:22:58 +00:00Commented Aug 9, 2020 at 0:22
1 Answer 1
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()])
-
2With FME use an AttributeTrimmer to remove any last ',' then a StringReplacer to replace ' ,' with nothing.Dan Iseminger– Dan Iseminger2020年08月09日 01:36:53 +00:00Commented 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– gisgis2020年08月09日 08:37:31 +00:00Commented Aug 9, 2020 at 8:37
-
@gisgis I don't use FME. Do you want me to delete my answer?user2856– user28562020年08月09日 08:59:57 +00:00Commented Aug 9, 2020 at 8:59
-
1no 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– gisgis2020年08月09日 09:04:18 +00:00Commented Aug 9, 2020 at 9:04