Select to view content in your preferred language

Field Calculator - Triming a field based on a specific Character

602
3
Jump to solution
12-08-2017 02:26 AM
THomasDanks
New Contributor II

Hi,

I've tried a few examples giving to people asking a similar question , but haven't managed to get any of them to work using the field calculator.

I basically have a shapefile with a field named "d_material" which contains the following entries:
DI - Ductile Iron
HDPE - High Density Polyethylene
HPPE - High Performance Polyethylene
CI - Cast Iron
MDPE - Medium Density Polyethylene
SI - Spun Iron
uPVC - Unplasticised Poly Vinyl Chloride
It also contains Null/blank cells

I'm trying to use the field calculator to remove text so I only end up with:
DI
HDPE
HPPE
CI
MDPE
SI
uPVC

Blank fields stay blank


I know its possible, but haven't managed to get it working. Any help would be great thanks. I'm running ArcMap v10.2.1 for desktop.

Regards,

Thomas

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

The logic I think you are looking for 

a = ['DI - Ductile Iron', None, 'HDPE - High Density Polyethylene',
     'HPPE - High Performance Polyethylene', 'CI - Cast Iron',
     'MDPE - Medium Density Polyethylene', None, 'SI - Spun Iron',
     'uPVC - Unplasticised Poly', 'Vinyl Chloride']

def cal(i):
    if i is None:
        v = None
    else:
        v = i.split(' ')[0]
    return v


for i in a:
    print(cal(i))
    
DI
None
HDPE
HPPE
CI
MDPE
None
SI
uPVC
Vinyl

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

The logic I think you are looking for 

a = ['DI - Ductile Iron', None, 'HDPE - High Density Polyethylene',
     'HPPE - High Performance Polyethylene', 'CI - Cast Iron',
     'MDPE - Medium Density Polyethylene', None, 'SI - Spun Iron',
     'uPVC - Unplasticised Poly', 'Vinyl Chloride']

def cal(i):
    if i is None:
        v = None
    else:
        v = i.split(' ')[0]
    return v


for i in a:
    print(cal(i))
    
DI
None
HDPE
HPPE
CI
MDPE
None
SI
uPVC
Vinyl
TedKowal
Regular Contributor II

Or something like this....

if s is not None:
      print s[:s.index('-')]
else:
      print ' '‍‍‍‍‍‍‍‍
THomasDanks
New Contributor II

Thanks guys this was a gret help. I got it working

0 Kudos