1 / 16

ABT 182 / HYD 182 Environmental Analysis using GIS Week 3-1

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'

marv
Download Presentation

ABT 182 / HYD 182 Environmental Analysis using GIS Week 3-1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ABT 182 / HYD 182 Environmental Analysis using GIS Week 3-1 Python list, string, function, and reading and writing text files

  2. String >>> s = 'Hello World' >>> s.replace('World', 'Folks') 'Hello Folks' >>> s.upper() 'HELLO WORLD' >>> s[0:5] 'Hello’

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

  4. query = ' "id" = 2 ' query = ' "name" = \' + Arizona + '\' '

  5. Tuples and Lists >>> a = (1,2,3) >>> type(a) <type 'tuple'> >>> b = [1,2,3] >>> type(b) <type 'list'>

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

  7. i = ['a', 'b', 'c'] i.append(4) i.extend(['hello', 'there']) i.insert(3, 'd') i.sort() i.reverse() i.count() i.len() Lists

  8. 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]

  9. 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]

  10. 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]

  11. Functions def prod(a,b): return(a * b) prod(3,5) prod = lambda a,b: a*b prod(3,5)

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

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

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

  15. ## 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]

  16. 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))

More Related