Skip to main content
Solved

create a personalised ID with letter and number


alc33
Contributor
Forum|alt.badge.img+10
  • Contributor

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.

Best answer by ebygomm

And an fme solution, ignoring zeros

View original
Did this help you find an answer to your question?

7 replies

david_r
Evangelist
  • October 18, 2017

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


ebygomm
Influencer
Forum|alt.badge.img+31
  • Influencer
  • Best Answer
  • October 18, 2017

And an fme solution, ignoring zeros


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • October 19, 2017

The NumToAlphaConverter from FME Hub could also be used here.


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • October 19, 2017
david_r wrote:

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 ;-)

 

 


david_r
Evangelist
  • October 19, 2017
takashi wrote:
Close! The range of number should be 1 to 9 ;-)

 

 

Ah, yes... But everyone knows that an array starts at 0, not 1 ;-)

david_r
Evangelist
  • October 19, 2017
takashi wrote:
Close! The range of number should be 1 to 9 ;-)

 

 

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

alc33
Contributor
Forum|alt.badge.img+10
  • Author
  • Contributor
  • October 19, 2017

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings