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.
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. 😉
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):
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. 😉
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)
and you tried...
return lookUp.get(str(elev),0)
Yes, it returns all zeros.
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.
Sure thing... if only you will tell me how to attach it here...(first time posting on here if you couldn't tell.)
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.
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.