在arcpy中将字符串字段转换为数字字段

2024-10-01 15:31:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大量(大于1000)的文件,其中包含定义为文本字段的数字的字段。我需要一个包含这些值作为数字的字段。我可以添加新字段,但当我无法填充它们时。在

我使用的是ArcGis 10.1。行的值可能在0-10之间,最多包含一个小数位,或者对于变量,它们可能是空的(实际上是空的,没有占位符)。在

下面是我为其中两个变量(N_CTN_CFY)使用的python脚本,以及我得到的错误。看来我的问题是如何将文本值转换为十进制转换。在

我不熟悉脚本,所以如果我的描述或单词选择不清楚,请原谅。在

import arcpy, os, sys
from arcpy import env
from decimal import *

arcpy.env.overwriteOutput = True

env.workspace = "C:\Users\OuelletteMS\Desktop\Ice_Data_testarea"

listFCs = arcpy.ListFeatureClasses("*")  
for fc in listFCs:
    print str("processing " + fc) # displays the file that is currently being handled

    strNCT = "N_CT"   # the current, text version of the field             
    newNCT = "NCT"    # the new, number version I want to create         
    strNCFY = "N_CFY" # the current, text version of the field           
    newNCFY = "NCFY"  # the new, number version I want to create         

    arcpy.AddField_management(fc,newNCT,"DOUBLE")
    arcpy.AddField_management(fc,newNCFY,"DOUBLE")

    cursor = arcpy.UpdateCursor(fc)
    for row in cursor:
        row.setValue(newNCT, row.getValue(Decimal(strNCT)))
        row.setValue(newNCFY, row.getValue(Decimal(strNCFY)))
        cursor.updateRow(row)

错误消息:

Runtime error Traceback (most recent call last): File "", line 23, in File "C:\Python27\ArcGIS10.1\Lib\decimal.py", line 548, in new "Invalid literal for Decimal: %r" % value) File "C:\Python27\ArcGIS10.1\Lib\decimal.py", line 3844, in _raise_error raise error(explanation) InvalidOperation: Invalid literal for Decimal: 'N_CT'


Tags: theinimportenvnewforversionrow
1条回答
网友
1楼 · 发布于 2024-10-01 15:31:47

可以使用以下方法将字符串值转换为浮点整数:

stringA = '12'
# Convert string to integer: 
int(stringA)
# Convert string to float 
float(stringA)

相关问题 更多 >

    热门问题