Select to view content in your preferred language

Insert a Zero after a specific number of character in a string

7147
10
Jump to solution
05-03-2013 09:33 AM
RobSmith
Emerging Contributor
I have a string field called [PROJECT_NO].  It has 7 characters in the string.  Example:  02WG028
We are changing our project number standard to eight characters, so I need to "insert" a zero after the 4th character from the left in the project number.
[ATTACH=CONFIG]24004[/ATTACH]
I have spent almost two whole days trying to figure this out on line with no luck.  My head is going to explode!

Thanks in advance for any help.

P.S. this layer is an SDE layer that I am editing using a version.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Try this in the field calculator, be sure to set the parser to Python:

Pre-logic code block
def AddZero(field):   new = field[:4] + "0" + field[4:]   return new;


And use this as the expression:
AddZero( !your_fieldname! )


[ATTACH=CONFIG]24010[/ATTACH]

View solution in original post

0 Kudos
10 Replies
by Anonymous User
Not applicable
Try this in the field calculator, be sure to set the parser to Python:

Pre-logic code block
def AddZero(field):   new = field[:4] + "0" + field[4:]   return new;


And use this as the expression:
AddZero( !your_fieldname! )


[ATTACH=CONFIG]24010[/ATTACH]
0 Kudos
RobSmith
Emerging Contributor
That worked great and now I feel stupid.  🙂

What if I wanted to throw in and if, then testing for the length of the PROJECT_NO string.  How would I do that.




Try this in the field calculator, be sure to set the parser to Python:

Pre-logic code block
def AddZero(field):
  new = field[:4] + "0" + field[4:]
  return new;


And use this as the expression:
AddZero( !your_fieldname! )


[ATTACH=CONFIG]24010[/ATTACH]
0 Kudos
by Anonymous User
Not applicable
You can add an if/then condition very easily with a length test.  What kind of conditions were you thinking of adding?

Here is an example of changing where to add the 0:

def AddZero(field):
  # add after 4th char if len == 7
  if len(field) == 7:
    new = field[:4] + "0" + field[4:]  # here is the "then" statement
  elif len(field) == 9:
    new = field[:5] + "0" + field[5:]  # inserts 0 @  'xxxxx0xxxx' (after fifth position)
  return new;
0 Kudos
RobSmith
Emerging Contributor
This worked out great.  I ended up adding conditions for 6,5,4 & 3 character project numbers.  Also I had a condition to just return the field if the project number was greater than 7.

Thanks for all your help.  This made my day.  I can now relax this weekend. :cool: 


You can add an if/then condition very easily with a length test.  What kind of conditions were you thinking of adding?

Here is an example of changing where to add the 0:

def AddZero(field):
  # add after 4th char if len == 7
  if len(field) == 7:
    new = field[:4] + "0" + field[4:]  # here is the "then" statement
  elif len(field) == 9:
    new = field[:5] + "0" + field[5:]  # inserts 0 @  'xxxxx0xxxx' (after fifth position)
  return new;
0 Kudos
by Anonymous User
Not applicable
I am trying to add in punctuation into one of my fields.  I saw the response above and tried it, but it only took the last line of code.  I also tried adding a "+" in between every one, but that didn't work either.  I don't have much of a coding background so I don't really know what else to do. 

I am attaching a screen shot of the code I wrote (that doesn't work).  I also have a screen shot of a sample area.

Original data- State_PIN
newpin- (I just did this manually, but I am trying to get a field calculator to add in the punctuation for me into the [formatedPIN] field)

Thanks for any help!
0 Kudos
JoshuaChisholm
Frequent Contributor
Laura,

You were SO close! Instead of using new= on each line you need to use new+=, otherwise you are replacing the variable new and starting from scratch on each line.

Also, I would take pieces of your string like this field[2:4]. That will grab the second and third characters.

def AddPunc(field):
 new=field[0:2]
 new+="-"+field[2:4]
 new+="-"+field[4:6]
 new+="-"+field[6:9]
 new+="-"+field[9:12]
 new+="-"+field[12:15]
 return new


Let me know how it goes. Good luck!
0 Kudos
by Anonymous User
Not applicable
Almost!!!  It is cutting out the last "-" I need though...

I tried adding another line:

new+="-"+field[15:18]

But that doesn't work.  Any ideas?

Thank you!!
0 Kudos
by Anonymous User
Not applicable
This worked for me:

code block:

from itertools import *

def formatPID(in_pid, delimiter='-', groups=()):
    # groups are how you want to break it up
    in_pid = str(in_pid)
    it = iter(in_pid)
    return delimiter.join(''.join(islice(it, i)) for i in groups)


Expression:
formatPID(!Your_PID_Field!, '-', (2,2,2,3,3,3))


EDIT: sorry, I forgot one group of 2's
0 Kudos
JoshuaChisholm
Frequent Contributor
Helle again Laura,

I missed the "." in your screen shot. Try this out:
def AddPunc(field):
 new=field[0:2]
 new+="-"+field[2:4]
 new+="-"+field[4:6]
 new+="-"+field[6:9]
 new+="-"+field[9:12]
 new+="."+field[12:15]
 new+="-"+field[15:18]
 return new


Note if you change [15:18] to [15:], the script will append any extra characters to the end of the new string.

If you're still having problems, make sure you're new field ('formatedPIN') has a length of at least 24 to allow for the new punctuation characters.

Good luck!
0 Kudos