Sie sind auf Seite 1von 12

String manipulation

and text files

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

print "Welcome to 'Writing Advanced Geoprocessing Scripts Using Python'"

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 < == >

 -1 indicates value not found

neCities = "Boston, Camden, Trentin, Queens, York"


if neCities.find("Trentin") > -1:
print "Should be Trenton"

>>> Should be Trenton

 upper() and lower()


print cities[0].upper()

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

 Converts 1st character to uppercase

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

streetType = sys.argv[1] # For example: Blvd


nameList = ["Main St", "First Av", "Redlands Blvd"]
for name in nameList:
if name.endswith(streetType):
print name.rstrip(streetType)

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

outFile = file(r"C:\temp\Documentation.txt", "w")

# Write a descriptive message


outFile.write("***** Strings *****" + "\n")

# Write the help for the find method


outFile.writelines("find " + string.find.__doc__ + "\n")
outFile.close()

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

 file()opens text file


 readline()reads a single line
 readlines()reads multiple lines
 close()releases the file from 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()

>>> 1 90589.286227 716031.87262


>>> 1 90824.1204555 716072.361256
>>> 1 91091.345662 716114.46951
...

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

 Discuss the difference between each set of functions


1)readlines() and writelines()# Read & write blocks of text

2)readline() and write()# Read & write a single line

A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 2-12

Das könnte Ihnen auch gefallen