2

I am trying to concatenate three fields into one using Python with the rjust padding function.

My function in the code box is the following:

def AutoSchluessel(gmk, zae, nen):
       return "{}0{}{}".format(gmk, str(zae).rjust(7,"0"), str(nen).rjust(5,"0")) 

And the Expression is:

flcode = AutoSchluessel( !gmkgcode!, !zaehler!, !nenner!)

What i am getting is for the fields is the following result:

gmkgcode = 5902; zaehler = 32; nenner = 17

flcode = 5902.0000032.0017.0

It should in my opinion though result in something without points (59020000003200017). What am I missing?

3

You need to force the number to not having any decimal digits. Adding int() will solve the problem I think. If the field that holds the original numbers has type of double, it is better to round the number to whole number before add int() function to them.

Here is a code that worked for me, but I tested it on two fields not three fields, as in your case, but you can add another field in the same way:

def AutoSchluessel(x,y):
    return "{0}{1}".format(str(int(round(x,0))).rjust(10,"0"), str(int(round(y,0))).rjust(12,"0"))

I have 2 Fields X = 246715.91 and Y = 4172742.24

After running the above code, I got 0000246716000004172742 (The desired output)

Without int(), I got the same results as yours 00246716.00004172742.0, which is incorrect

  • Thanks! Yes, my bad, the input fields indeed were "Double" which I didn't even check because it makes no sense as there will only be integers in them. @PolyGeo: I marked this as the accepted answear as the code improvements are shown here as well. – Matze Jun 9 '17 at 10:28
3

Your Python code seems valid because when I run:

def AutoSchluessel(gmk, zae, nen):
       return "{}0{}{}".format(gmk, str(zae).rjust(7,"0"), str(nen).rjust(5,"0"))

gmk = 5902
zae = 32
nen = 17

flcode = AutoSchluessel(gmk, zae, nen)

print flcode

it prints out 59020000003200017

If your three input fields are integer then I would expect the same result within the field calculator.

However, if one of those fields is a float or double then I could imagine the points being introduced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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