简体   繁体   English

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

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

I'm trying to do a field calculation in ArcMap from one field to another.我正在尝试在 ArcMap 中从一个字段到另一个字段进行字段计算。 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:我的起始值(来自 StatedArea 字段)是这样的:
12.99 a 12.99 一
0.2377 a 0.2377 一
0.0041 a 0.0041 一

I just want to take off the space and the "a".我只想去掉空格和“a”。

All I'm getting is ERROR 000539, with this message "ValueError: could not convert string to float"我得到的只是错误 000539,并带有此消息“ValueError:无法将字符串转换为浮点数”

Any ideas what the issue could be?任何想法可能是什么问题?

float(!StatedArea![:-2])

should work.应该管用。 You might want to revisit list indexing in python: https://www.tutorialspoint.com/python/python_lists.htm您可能想重新访问 Python 中的列表索引: https : //www.tutorialspoint.com/python/python_lists.htm

As already stated [:-2] will discard two last characters of str .如前所述[:-2]将丢弃str最后两个字符。 I want to note that Python has no built-in double type.我想指出的是,Python 没有内置的double类型。 According to documentation in most implementations, Python's float is equivalent to C double .根据大多数实现中的文档,Python 的float等效于 C double Therefore if you will use float , you might expect precision equivalent to double .因此,如果您将使用float ,您可能期望精度相当于double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM