160 likes | 235 Views
ABT 182 / HYD 182 Environmental Analysis using GIS Week 3-1. Python list, string, function, and reading and writing text files. String. >>> s = 'Hello World' >>> s.replace('World', 'Folks') 'Hello Folks' >>> s.upper() 'HELLO WORLD' >>> s[0:5] 'Hello’. >>> s = 'Hello World'
E N D
ABT 182 / HYD 182 Environmental Analysis using GIS Week 3-1 Python list, string, function, and reading and writing text files
String >>> s = 'Hello World' >>> s.replace('World', 'Folks') 'Hello Folks' >>> s.upper() 'HELLO WORLD' >>> s[0:5] 'Hello’
>>> s = 'Hello World' >>> s.split(' ') ['Hello', 'World'] >>> s.find('World') 6 >>> s.find('Robert') -1 >>> s.upper() 'HELLO WORLD' s.replace(), s.rstrip(), s.lstrip()
query = ' "id" = 2 ' query = ' "name" = \' + Arizona + '\' '
Tuples and Lists >>> a = (1,2,3) >>> type(a) <type 'tuple'> >>> b = [1,2,3] >>> type(b) <type 'list'>
Lists and Loops x = 0 for i in range(5): x = x + i x = 0 for i in [0, 1, 2, 3, 4]: x = x + i
i = ['a', 'b', 'c'] i.append(4) i.extend(['hello', 'there']) i.insert(3, 'd') i.sort() i.reverse() i.count() i.len() Lists
List comprehensions >>> M = [1,2,3,4,5,6,7,8,9] >>> [x + 10 for x in M] [11, 12, 13, 14, 15, 16, 17, 18, 19] >>> M = [[1,2,3], [4,5,6], [7,8,9]] >>> [x[0] + 10 for x in M] [11, 14, 17]
M = [1,2,3,4,5,6,7,8,9] for i in range(len(M)): M[i] = M[i] + 10 M = [x + 10 for x in M]
List comprehensions >>> M = [[1,2,3], [4,5,6], [7,8,9]] >>> [x[1]for x in M if x[1] % 2 == 0] [2, 8]
Functions def prod(a,b): return(a * b) prod(3,5) prod = lambda a,b: a*b prod(3,5)
Reading from text files • Open in read mode "r" and assign to variable f = open('c:/ABT182/file.txt', "r") • Read • s = f.readline() • s = f.readlines(5) • s = f.read • Close file • f.close()
f = open("d:/file.txt", "r") for line in f.readlines(): print line line = f.readline() while line <> "": print line line= f.readline() f.close()
Writing to text files f = open('c:/ABT182/file.txt', "w") Creates new file for writing "w" (alternative is append "a") f.write('Dear Amy\n') writes one line in an opened file f.writelines(['How are you?\n', 'See you soon\n']) writes many lines in an opened file f.close() releases the file from memory
## Author: Matthew Hamilton ## Date: April 10, 2010 ## Data source: http://www.nobochamp.com/capitals.htm ## Purpose: code will ….. ## Create the object statecapitals statecapitals = [['State', 'Capitals', 'Alt[m]', 'Lat', 'Lon'], ['Alabama', 'Montgomery', '75', '32.3754', '-86.2996'], etc. ## Create the numeric lat & lon objects lat = [float(i[3]) for i in statecapitals[1:]] lon = [float(i[4]) for i in statecapitals[1:]] ## Finding the northernmost city Nlat = max(lat) # lat.sort(reverse=True) # Nlat = lat[0] ## Iterates over range of the length of observations for m in statecapitals[1:]: ## strings should be converted to numeric data if float(m[3]) == float(Nlat): print "Northernmost city is: " + m[1] + ", " + m[0]
import sys # Using arguments to select a bounding box; S = sys.argv[1] N = sys.argv[2] W = sys.argv[3] E = sys.argv[4] # Communicating the extent print "Your bounding box is (SN), (WE) is (" + S + "," + N + "),(" + W + "," + E + ")" # Make a subset object subset = list() for e in statecapitals: ## testing if inside box if float(e[3]) < float(N) and float(e[3]) > float(S) and float(e[4]) < float(E) and float(e[4]) > float(W): subset.append(e) # print the count of records print 'number of records: '+str(len(subset))