-1

I'm trying to do a field calculation in ArcMap from one field to another. I need to slice off the last two characters of one field, and convert to float (or double).

I'm trying to do this:

float(!StatedArea![-2])

My starting values (from the StatedArea field) are like this:
12.99 a
0.2377 a
0.0041 a

I just want to take off the space and the "a".

All I'm getting is ERROR 000539, with this message "ValueError: could not convert string to float"

Any ideas what the issue could be?

  • I don't know ArcMap, thus I will assume the notation `!StateArea!` is proper to ArcMap. To take off the 2 last element from a string, you have to use `:`. `str[:len(str)-2]`. The `[-2]` select the element before the last one, e.g. `"abcdef"[-2]` returns `"e"` – Mathieu Apr 15 '19 at 17:38
  • you would want to grab all values upto the last 2, this can be done with `[:-2]` – Ely Fialkoff Apr 15 '19 at 17:39

2 Answers2

0
float(!StatedArea![:-2])

should work. You might want to revisit list indexing in python: https://www.tutorialspoint.com/python/python_lists.htm

emilaz
  • 1,722
  • 1
  • 15
  • 31
0

As already stated [:-2] will discard two last characters of str. I want to note that Python has no built-in double type. According to documentation in most implementations, Python's float is equivalent to C double. Therefore if you will use float, you might expect precision equivalent to double.

Daweo
  • 31,313
  • 3
  • 12
  • 25