Select to view content in your preferred language

Passing Value for Dictionary Look Up Fails

4581
13
Jump to solution
12-21-2014 07:36 PM
AmySorensen
Occasional Contributor

I've a field calculator that I am trying to use that uses a csv file to look up appropriate value based on key that is passed provided to the function. As provided below, the only values calculated are zero. The code itself is loading the data and working correctly. If I enter any number into final line of code instead of "elev" it calculates the appropriate value. If I just return "elev" to see if value is getting there, it calculates the correct values. When I try the code as below, I can't get it to return the right values. Any suggestions would be appreciated.

Using ArcGIS 10.2 at ArcInfo level

What I have is below:

  import csv 
     def IDSeg(elev): 
      with open('path', mode='r') as infile:
      reader = csv.reader(infile) 
      for rows in reader: 
       k = rows[0] 
       v = rows[1] 
       lookUp = {rows[0]:rows[1] for rows in reader} 
       return lookUp.get(elev,0)
0 Kudos
1 Solution

Accepted Solutions
TomGeo
by
Frequent Contributor

Hi Amy,

did you check on the type of the 'elev' that gets returned by your function?

csv.reader will give you the values as strings. Hence, there is the possibility that you are passing the correct value to dict.get() but its type is string. If I understand your explanation correct what you want is a type 'int'.

type(rows[0])

will give you the type of your key. the type of 'elev' and that of rows[0] have to be the same.

Cheers Thomas

btw.: that's pure Python and independent from any ArcGIS version. 😉

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!

View solution in original post

13 Replies
DanPatterson_Retired
MVP Emeritus

Can you format you code using Python syntax (turn on the Advanced editor, select >>, then syntax highlighting, then Python...I know it buried in the depths of hell but that is where it is)

It is hard to see if your indentation is correct, but one glaring error is that the import statement and the def should have the same indentation

import csv

def (blah):

TomGeo
by
Frequent Contributor

Hi Amy,

did you check on the type of the 'elev' that gets returned by your function?

csv.reader will give you the values as strings. Hence, there is the possibility that you are passing the correct value to dict.get() but its type is string. If I understand your explanation correct what you want is a type 'int'.

type(rows[0])

will give you the type of your key. the type of 'elev' and that of rows[0] have to be the same.

Cheers Thomas

btw.: that's pure Python and independent from any ArcGIS version. 😉

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
AmySorensen
Occasional Contributor

It appears that 'elev' is passed as a float and  rows[0] is a string.

I've edited the code and tested values and but I'm still not getting the correct value pulled when I try to pass the new, string value x into the function.

import csv
def IDSeg(elev):
 with open('C:\\SD.csv', mode='r') as infile:
     x = str(elev)
     reader = csv.reader(infile)
     for rows in reader:
          k = rows[0]
          v = rows[1]
          lookUp = {rows[0]:rows[1] for rows in reader}
          return lookUp.get(elev,0)

On line 10, if I substitute any of the key values my file contains directly as below it does grab the correct value.

          return lookUp.get('9970', 0)
0 Kudos
DanPatterson_Retired
MVP Emeritus

and you tried...

return lookUp.get(str(elev),0)

0 Kudos
AmySorensen
Occasional Contributor

Yes, it returns all zeros.

0 Kudos
TomGeo
by
Frequent Contributor

Can you provide us with the csv file? At this point I could only throw around wild guesses and would give it a shot myself.

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
AmySorensen
Occasional Contributor

Sure thing... if only you will tell me how to attach it here...(first time posting on here if you couldn't tell.)

0 Kudos
TomGeo
by
Frequent Contributor

Go to the advanced editor... lower right corner provides a possibility to attach a file. If you can't add the csv file, then zip it and upload the zip.

Looking forward.

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
AmySorensen
Occasional Contributor

I'm going to mark this as the correct answer, even though there is still the odd behaviour that I can not get it to run in 10.2 and I can at 10.1. I'm not sure what could be the cause of this but the typecasting was also needed.

0 Kudos