Beruflich Dokumente
Kultur Dokumente
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python
Learning objectives
Write code to read and write text files
Modify a string
Parse a string into usable parts
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-2
What you should already know about strings
Strings surrounded in double (" ") or single (' ') quotes
Can embed one string in another
Strings can be combined with the + operator
Paths are strings
Strings are zero-based
Strings are indexed
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-3
Manipulating strings
split() find() Conditional
operators
Returns a Python list Returns start position < == >
>>> BOSTON
A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-4
Changing the contents of a string
replace()
inValue = "Catalina.shp"
Substitutes a value outValue = inValue.replace(".shp", "")
print outValue
>>> Catalina
capitalize()
city = "milwaukee"
print city.capitalize()
>>> Milwaukee
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-5
Changing the contents of a string (cont’d)
lstrip(), rstrip() endswith()
Strips off from left or right Finds trailing characters
>>> Redlands
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-6
Writing to a text file
Uses for text files Open in write or append mode
Write line by line or paragraphs
Log process steps and track time Close file to save it
Create reports
Join tables
Create address labels after geocoding
Useful methods
file()creates new file or opens existing file
write()creates one line
writelines()creates a paragraph
close()releases the file from memory
Newline character \n
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-7
Code example: Write to a text file
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-8
Reading a text file
File must exist Open in read mode
Read line by line (or several lines)
Useful methods Close file to release memory
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-9
Code example: Read a text file
textFile = r"C:\Student\WAGS\Database\TrailsCoords.txt"
inFile = file(textFile, "r")
# Read entire file into line strings
for line in inFile.readlines():
print line
inFile.close()
# Or read one line at a time
sLine = inFile.readline()
while sLine <> "":
print sLine
sLine = inFile.readline()
A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-10
Exercise goals
Open a text file and read its contents
Modify values
Write values to new file
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-11
Lesson review
List the modes for opening text files
read
1) __________ write 3) ___________
2) __________ append
Name the function that converted inStr to var<n>
inStr = "MiNnEaPoLiS"
var1 = "MINNEAPOLIS" upper()
var2 = "Minneapolis" capitalize()
var3 = "minneapolis" lower()
var4 = "MiNnE" split("a")[0]
var5 = "Minnesota" lower().replace("apolis","sota").capitalize()
A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-12