Solved

Splitting an interger attribute into many raws?


Badge +6
  • Contributor
  • 19 replies

Hi guys, I have an integer attribute containing all node_ids. I need to split those ids apart. The number of integers for all ids is similar (e.g. 7 integers). I came to this attribute by extracting the ids of the child nodes from a json file. I used this expresion (json["data"][*]["id"]), I used the * here because I have so many ids and I need them all. The result is one attribute with all node ids without any seperation between them (which i could have used to split them). So, the result e.g. is as following (72523657255486725577872521357256895). I know that I can use the attribute splitter to seperate them as the following picture but I need to be able to split them using some kind of expersion which split each 7 integers at once because I might have handreds of ids and it is not practical to keep counting them and then use 7s equivalent to their number. Therefore, my question is: Is there a way to add a comma or semicolon between the node ids when I extract them from the JSON file? or Is there any regular expression which I can use in the attribute splitter to do this trick automatically? Or is there any other transformer which will help?

Thanks a lot for your help in advance,

icon

Best answer by takashi 29 April 2019, 18:13

View original

5 replies

Badge +10

You could use a stringsearcher with the regular expression (make sure you set the All matches list name under advanced parameters)

 \d{7}

followed by a list concatenator, to put back together separated by commas.

 

Or a python caller

import fme
import fmeobjects

def processFeature(feature):
    number = feature.getAttribute('longnumber')
    n = 7
    split =  ([number[i:i+n] for i in range(0,len(number),n)])
    numberwithcomma = ','.join(split)
    feature.setAttribute('longnumber',numberwithcomma)
Badge +1

You could use a stringsearcher with the regular expression (make sure you set the All matches list name under advanced parameters)

 \d{7}

followed by a list concatenator, to put back together separated by commas.

 

Or a python caller

import fme
import fmeobjects

def processFeature(feature):
    number = feature.getAttribute('longnumber')
    n = 7
    split =  ([number[i:i+n] for i in range(0,len(number),n)])
    numberwithcomma = ','.join(split)
    feature.setAttribute('longnumber',numberwithcomma)

To expand on that, there is also the option to just split evert nth character

https://stackoverflow.com/questions/9475241/split-string-every-nth-character

You could then use the FME Objects in the python call to create a new object under the while (for i in) and that would output the record each time the 7th was hit.  Just make sure to set a new attributes with the 7 integers after you create the new feature.

https://knowledge.safe.com/questions/40316/create-new-features-within-a-python-caller.html

Badge +6

@egomm Thank a lot for your quick response. Indeed it worked out using the first two transformers you mentioned :).

Also Thanks a lot @dellerbeck for your reponse.

Userlevel 2
Badge +17

Alternatively the StringReplacer with this settings could also be helpful.

  • Mode: Replace Regular Expression
  • Text to Replace: (?<=\\d{7})(\\d{1,7})
  • Replacement Text: ,\\1
Badge +6

Thanks a lot @takashi. This solution worked just fine :)

Reply