Select to view content in your preferred language

Passing Value for Dictionary Look Up Fails

4612
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
13 Replies
AmySorensen
Occasional Contributor

I think it is a bug.

I just tried it at 10.1 on my local desktop and it ran perfectly.

0 Kudos
TomGeo
by
Frequent Contributor

Does it even work without the type casting from float to string?

Don't know... Never had a problem with the csv module or any fundamental function in Python 2.6 or later. Neither in an installation of ArcGIS nor a standalone Python installation.

- 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

It does not work with the type casting, so that was needed.

Running the same bit of code in both locations though and it returns all '0' in 10.2 on citrix and correct values on 10.1 on local.

Thanks for all your help!

0 Kudos
TomGeo
by
Frequent Contributor

Could you mark my suggestion for type casting then as the solution to your question. Your first question, my first answer. 😉

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