Avidgeo String Manipulation : Getting Started with Python and ArcGIS
Post on 16-Jul-2015
525 Views
Preview:
Transcript
String Manipulation
Getting started with python and ArcGIS
Guido Steinguido@avidgeo.com
AvidGeo Meetup 2/21/2013
Guido Stein
● GIS AnalystApplied Geographics, Inc.○ Parcel Editing○ Extract, Transform, and Load○ Workflows
■ Model Builder■ Calculate Field■ Python
Overview
● Municipal Data● Variables● Fields● Strings As Lists● String Functions● More About String● Concatenation● Putting It Together● Single Line Statement If/Else● Next Time
It's Raining Data
Planimetric Data○ Road○ Building○ Fire Hydrant
Cadastral Data○ Municipal Boundaries○ Real Estate
■ Parcel■ Property
More Data More Problems
Parcel Polygon
045-267 / 88-3.A045-267/ 88-4045-267 / 88-5045- 267/88-6145- 267/88-7A145- 267/88-7B
Property Table
45-88-3-A45-88-445-88-545-88-6145-88-7A145-88-7B
String"guido"
Integer35
Float5.75
List["Burrito","Dorito"]
Variable Declarationme = "guido"age = 35height = 5.75sounds_like = ["Burrito","Dorito"]
Me, a name I call myself
Parseltongue Translator
VB
[parcel_id]
trim([parcel_id])
"N" & [parcel_id]
Python
!parcel_id!
!parcel_id!.strip()
"N" + !parcel_id!
Cutting the Cheese
cheese = "Gorgonzola"
["G","o","r","g","o","n","z","o","l","a"]
cheese[:4]"Gorg"● Like left() in VB
cheese[-4:]"zola"● Like right() in VB
cheese[4:-4]"on"● Like mid() in VB
Functional Design 1
● strip(), lstrip(), rstrip()● like VB trim()
par = "045-267 / 88-3.A"
par[:8]"045-267 "
par[:8].strip()"045-267"
par.lstrip("0")"45-267 / 88-3.A"
par.strip(" 0.3A-8/")"45-267"
Functional Design 2
● split()par = "045-267 / 88-3.A"
par.split('/')["045-267 "," 88-3.A"]
par.split('/')[0]"045-267 "
● replace()
par.replace('.','-')"045-267 / 88-3-A"
Now for something completely
different
Escape\New Line\nsingle/double quote\' \"
If you use single quote around string literal then double quotes don't need to be escaped
Vise versa
Concatenate
par = '045-267 / 88-3.A'
!par![1:3] + '-' + !par![4:7]'%s-%s' % (!par![1:3],!par![4:7])'45-267'
● %s - string● %i - integer● %.2f - float with 2 decimal points
Putting it together
par = '045-267 / 88-3.A'
'%s-%s' % (par[1:3],par.split('/')[1].replace('.','-').strip()
)
'45-88-3-A'
Putting it together for real
'%s-%s' % ( !parcel_id![1:3], !parcel_id!.split('/')[1].replace('.','-').strip())
More Data More Problems
Parcel Polygon
045-267 / 88-3.A045-267/ 88-4045-267 / 88-5045- 267/88-6145- 267/88-7A145- 267/88-7B
Property Table
45-88-3-A45-88-445-88-545-88-6145-88-7A145-88-7B
Putting it together for real, really
'%s-%s' % ( !parcel_id!.split('/')[0].strip(' 0'), !parcel_id!.split('/')[1].replace('.','-').strip())
Putting it together for real, really,
really
'%s-%s' % ( !parcel_id!.split('/')[0].strip(' 0').split('-')[0], !parcel_id!.split('/')[1].replace('.','-').strip())
One More thing
Null = None It will crash your statement
alternate statement if field is None else primary statement
'' if !parcel_id! is None else ('%s-%s' % ( !parcel_id!.split('/')[0].strip(' 0').split('-')[0], !parcel_id!.split('/')[1].replace('.','-').strip()))
Next Time
Advanced Calculate● building your own functions● tabbing● if,then, else, elif● import libraries
○ re● More string functions● variable comparison
top related