format field

1313
16
Jump to solution
08-28-2020 11:57 AM
2Quiker
Occasional Contributor II

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

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])‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

16 Replies
RandyBurton
MVP Alum

A quick example (using underscore for space):

rows = [['W1234500000',],
        ['W1234501000',],
        ['W12345010S0',],
        ['W12345010S1',] ]

for row in rows:
    if row[0][10] == '0':
        print(row[0][:9]+"_"+row[0][-1:])
    else:
        print(row[0])

prints:
W12345000_0
W12345010_0
W12345010_0
W12345010S1‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2Quiker
Occasional Contributor II

I am getting error string index out of range on line 5.

Also the "_" to " " because i need the space not the underscore.

dict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(fc,['WellID','WPin'])}

with arcpy.da.UpdateCursor(fc,['WellID','WPin']) as cursor: 
    for row in cursor:
        if row[0][10] == '0':
            #print(row[0][:9]+"_"+row[0][-1:])
            row [1] = row[0][:9]+" "+row[0][-1:]
        else:
            print(row[0])
        cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍
0 Kudos
DanPatterson
MVP Esteemed Contributor
w0 = "W1234500000"
w1 = "W12345010S0"

import string
nums = string.digits

def conv(fld, nums):
    """docstring"""
    if fld[-2] in nums:
        return "{} {}".format(fld[1:-2], fld[-1:])
    else:
        return fld[1:]
        

conv(w0, nums)
'12345000 0'

conv(w1, nums)
'12345010S0'

... sort of retired...
2Quiker
Occasional Contributor II

I am kind of lost on this one because it's not formatted with updatecusror, can you explain what's going on please?

0 Kudos
DanPatterson
MVP Esteemed Contributor

Use the Calculate Field or the just the field calculator.  I see little reason to use a search cursor on this.

besides, your 'row' is my 'fld' and I use a different means of slicing that relies less on string length but more on string head/tail positions of the things to change.  I am sure you will see upon closer examination


... sort of retired...
0 Kudos
2Quiker
Occasional Contributor II

I am getting error 000989. If and else have two indent and the return have 4 indents.

Field Calculator

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

 Code Block

def conv( WellID , nums)

0 Kudos
DanPatterson
MVP Esteemed Contributor

indent it properly then, like in my example


... sort of retired...
0 Kudos
2Quiker
Occasional Contributor II

I have but for some reason it won't work no matter how many spaces(indents) I put in, in line 6 gives me the same error.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

One problem you have is with your variable names.  You don't put the field name in exclamations in the function definition.  For example, don't do !WellID! and instead do WellID.

0 Kudos