Skip to main content

Hello,

I would like to create a personalised ID (counter personalised) with letters and number.

I use 2 letters (A to Z) and 1 number (1 to 9), so I would like something like that :

AA1

AA2

AA3

...

AB1

AB2

AB3

...

I don't know how to do that and I don't find the solution on the web. Maybe a python script? But I haven't no knowledge on this langage.

If you have any idea...

Thank you for your future answer.

Here's a quick and dirty solution using a Python script:

import fmeobjects
 
class AlphanumericCounter(object):
    def __init__(self):
        self.counter = 0
       
    def counter_as_str(self):
        a = self.counter // (26*9)
        b = (self.counter - (a*26*9)) // 9
        c = self.counter - (a*26*9) - (b*9)
        return chr(65+a) + chr(65+b) + str(c+1)
        
    def input(self,feature):
        feature.setAttribute('id', self.counter_as_str())
        self.counter += 1
        if self.counter > (26*26*9):
            raise fmeobjects.FMEException('Too many features!')
        self.pyoutput(feature)

Each output feature will have a new attribute "id" that contains the counter.

If the number of features exceeds 6084 (id > ZZ9), it will terminate the translation with an error message. PythonCaller parameters:

0684Q00000ArLrUQAV.png


And an fme solution, ignoring zeros


The NumToAlphaConverter from FME Hub could also be used here.


Here's a quick and dirty solution using a Python script:

import fmeobjects
 
class AlphanumericCounter(object):
    def __init__(self):
        self.counter = 0
       
    def counter_as_str(self):
        a = self.counter // (26*9)
        b = (self.counter - (a*26*9)) // 9
        c = self.counter - (a*26*9) - (b*9)
        return chr(65+a) + chr(65+b) + str(c+1)
        
    def input(self,feature):
        feature.setAttribute('id', self.counter_as_str())
        self.counter += 1
        if self.counter > (26*26*9):
            raise fmeobjects.FMEException('Too many features!')
        self.pyoutput(feature)

Each output feature will have a new attribute "id" that contains the counter.

If the number of features exceeds 6084 (id > ZZ9), it will terminate the translation with an error message. PythonCaller parameters:

0684Q00000ArLrUQAV.png

Close! The range of number should be 1 to 9 ;-)

 

 


Close! The range of number should be 1 to 9 ;-)

 

 

Ah, yes... But everyone knows that an array starts at 0, not 1 😉
Close! The range of number should be 1 to 9 ;-)

 

 

But thanks for the tip, I've fixed the code above

Hello,

Thank for all yours replies and for your reactivity!

I choose to evacuate the 0 for a specific reason, that's why I ask only 1 to 9 😉 thanks david_r for the adaptation of your code :)

Have a good day everyone!


Reply