Trying to format a field but I am not sure how accomplish it. The trouble I am having is the making the second to last 0 a space but only if there is no alph char. WellPin field is always 10 numbers and sometimes with an alph char. Can someone help me out by showing me how to do this with updatecurosr and in a filed calculator please?
Well WPin
W1234500000 --> 12345000 0
W1234501000--> 12345010 0
W12345010S0--> 12345010S0
W12345010S1--> 12345010S1
Here what I have so far.
import arcpy
arcpy.env.overwriteOutput = True
fc = r"C:\Temp\Wells.shp"
count = 0
def maketv_cursor(fc):
rows = arcpy.SearchCursor(table,"WPin")
count = 0
for row in rows:
count += 1
#print(count)
with arcpy.da.UpdateCursor(fc,['WellID','WPin']) as cursor:
for row in cursor: #if row[0] in (None, "", " "):
row [1] = (row[0][1:11]) # Field Calculator!WellID! [1:11]
cursor.updateRow(row)
Solved! Go to Solution.
After your suggestion I am getting error 000539 invalid syntax expression line 1.
Looking at your screenshot from earlier, you are also trying to define a function in the expression box.
Your expression should be:
conv(!WellID!)
and your pre-script code should be:
import string
nums = string.digits
def conv(WellID):
"""docstring"""
if WellID[-2] in nums:
return "{} {}".format(WellID[1:-2], WellID[-1:])
else:
return WellID[1:]
If it has to be an update cursor, try:
import arcpy
fc = r"C:\Temp\Wells.shp"
with arcpy.da.UpdateCursor(fc,['WellID','WPin']) as cursor:
for wellid, wpin in cursor:
if wellid not in (None, "", " "):
i,j,k = wellid[:-2], wellid[-2], wellid[-1:]
wpin = "{}{}{}".format(i, j if not j.isdigit() else " ", k)
cursor.updateRow([wellid, wpin])
Joshua Bixby If the goal is to keep the letter and drop the zero from the next to last position, shouldn't line 9 be ("not" isdigit) ?
wpin = "{}{}{}".format(i, j if not j.isdigit() else " ", k)
Randy Burton, good catch, I got mixed up on what the OP was after. You are correct, I have updated my code snippet. Thanks.
This worked, although would it be best to do just do it through Calculate field management.
import string
nums = string.digits
def conv( WellID , nums):
"""docstring"""
if !WellID! [-2] in nums: # two space indents
return "{} {}".format( !WellID! [1:-2], !WellID! [-1:])#four spaces indents
else: # two space indents
return !WellID! [1:]#four spaces indents