I have two types of number in my shapefile/database in the same column as follows. The first one does not have any alphabetic character but second one has alphabetic characters.

(a) 26 25 21 0000 00200 0000
(b) 27 25 21 0050 00000 001A

There is one space between the blanks as it can be seen from the number. The number given are mismatched. Here, in the first example, the 21 should take the place of 26 and 26 should take the place of 21 and in second example, 21 should take place of 27 and vice-versa. For example, this is the output I am after:

(a) 2125260000002000000
(b) 212527005000000001A

How can I replace those numbers using the field calculator?

  • Welcome to GIS SE. Your question is an issue related to regular expressions, doesn't have a GIS component. You can ask in stackoverflow instead – aldo_tapia Mar 26 '18 at 18:49
  • @aldo_tapia I see this as more of a field calculator problem, which, in my opinion, is relevant to this forum. I made a few edits to hopefully address your concerns. – Aaron Mar 26 '18 at 20:07
  • Thank you gusy, but it was a GIS problem. However, it has been solved by @ahmadhanb. – BPS Mar 26 '18 at 20:58

You can achieve this using Field Calculator by creating new field and apply the following python code in the Pre-Logic Script Code:

def mod_txt(f):
    txt = f.split()
    if len(txt) > 0:
        newtxt = str(txt[2])+str(txt[1])+str(txt[0])+str(txt[3])+str(txt[4])+str(txt[5])
        return newtxt
        return ''

Write in the new field the following:

mod_txt( !FieldName! )

The code will split the text field using the spaces and will rearrange the string based on your requirement to replace the first number with the third one and vice-versa.

enter image description here

Here is the result:

enter image description here

  • Thank you so much@ahmadhanb for your help. It worked very nicely with a blink of eye. I spent several hours to figure it out. – BPS Mar 26 '18 at 20:56
  • You are welcome. Glad it worked :) – ahmadhanb Mar 27 '18 at 6:10

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.