Top Banner
String Manipulation Getting started with python and ArcGIS Guido Stein [email protected] AvidGeo Meetup 2/21/2013
20

Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Jul 16, 2015

Download

Technology

Guido Stein
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

String Manipulation

Getting started with python and ArcGIS

Guido [email protected]

AvidGeo Meetup 2/21/2013

Page 2: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Guido Stein

● GIS AnalystApplied Geographics, Inc.○ Parcel Editing○ Extract, Transform, and Load○ Workflows

■ Model Builder■ Calculate Field■ Python

Page 3: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Overview

● Municipal Data● Variables● Fields● Strings As Lists● String Functions● More About String● Concatenation● Putting It Together● Single Line Statement If/Else● Next Time

Page 4: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

It's Raining Data

Planimetric Data○ Road○ Building○ Fire Hydrant

Cadastral Data○ Municipal Boundaries○ Real Estate

■ Parcel■ Property

Page 5: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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

Page 6: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

String"guido"

Integer35

Float5.75

List["Burrito","Dorito"]

Variable Declarationme = "guido"age = 35height = 5.75sounds_like = ["Burrito","Dorito"]

Me, a name I call myself

Page 7: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Parseltongue Translator

VB

[parcel_id]

trim([parcel_id])

"N" & [parcel_id]

Python

!parcel_id!

!parcel_id!.strip()

"N" + !parcel_id!

Page 8: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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

Page 9: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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"

Page 10: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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"

Page 11: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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

Page 12: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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

Page 13: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Putting it together

par = '045-267 / 88-3.A'

'%s-%s' % (par[1:3],par.split('/')[1].replace('.','-').strip()

)

'45-88-3-A'

Page 14: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Putting it together for real

'%s-%s' % ( !parcel_id![1:3], !parcel_id!.split('/')[1].replace('.','-').strip())

Page 15: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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

Page 16: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Putting it together for real, really

'%s-%s' % ( !parcel_id!.split('/')[0].strip(' 0'), !parcel_id!.split('/')[1].replace('.','-').strip())

Page 17: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Putting it together for real, really,

really

'%s-%s' % ( !parcel_id!.split('/')[0].strip(' 0').split('-')[0], !parcel_id!.split('/')[1].replace('.','-').strip())

Page 18: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

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()))

Page 19: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

Next Time

Advanced Calculate● building your own functions● tabbing● if,then, else, elif● import libraries

○ re● More string functions● variable comparison

Page 20: Avidgeo String Manipulation : Getting Started with Python and ArcGIS

String Manipulation

Getting started with python and ArcGIS

Guido [email protected]

AvidGeo Meetup 2/21/2013