如何切掉字符串中的最后两个字符然后转换为双字符?

How to slice off last two characters in a string then convert to a double?

我正在尝试在 ArcMap 中从一个字段到另一个字段进行字段计算。我需要切掉一个字段的最后两个字符,并转换为 float(或 double)。

我正在尝试这样做:

float(!StatedArea![-2])

我的起始值(来自 StatedArea 字段)是这样的:
12.99 一个
0.2377
0.0041

我只想取下 space 和 "a"。

我得到的只是错误 000539,以及此消息 "ValueError: could not convert string to float"

知道问题出在哪里吗?

float(!StatedArea![:-2])

应该可以。 您可能想重新访问 python 中的列表索引: https://www.tutorialspoint.com/python/python_lists.htm

如前所述,[:-2] 将丢弃 str 的最后两个字符。我要注意 Python 没有内置的 double 类型。根据 documentation 在大多数实现中,Python 的 float 等同于 C double。因此,如果您将使用 float,您可能期望精度等同于 double.