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)
Solved! Go to Solution.
I think it is a bug.
I just tried it at 10.1 on my local desktop and it ran perfectly.
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.
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!
Could you mark my suggestion for type casting then as the solution to your question. Your first question, my first answer. 😉