2013-12-22 55 views
0

有人可以告訴我如何進行簡單的字段計算,比如在ArcPy中從「A」複製到「B」?我在網上發現了很多例子,他們都使用額外的表達方式,而且它們很複雜。我需要的是像ArcMap中桂:ArcPy中的簡單字段計算

B = !A! 

更新時間: 這裏是我迄今,但我得到的錯誤上運行此作爲

NameError: name 'A' is not defined

point_shp = "G:\\Temp\\All_Provinces.shp" 
arcpy.AddField_management(point_shp, "B", "TEXT", "", "", "25", "", "NON_NULLABLE", "NON_REQUIRED", "") 
arcpy.CalculateField_management(point_shp, "B", "A", "PYTHON_9.3") 
+0

您可以發佈其中一個更復雜的示例嗎? –

回答

0

代碼好吧,我知道了,A必須包裝好像"!A!"

arcpy.CalculateField_management(point_shp, "B","!A!", "PYTHON_9.3") 

哇這個論壇太棒了!這麼多幫助!

1

可選,如果你想計算字段工具來計算使用VB,那麼你可以這樣做:

arcpy.CalculateField_management(point_shp, "B","[A]") 

這只是如果你有一個視覺的基本要求,而不是一個應用程序。

0

我會通過使用更新遊標來攻擊這個,儘管我不知道這是否符合您對簡單性的需求。

point_shp = "G:\\Temp\\All_Provinces.shp" 
updateCursor = arcpy.UpdateCursor(point_shp) 
for row in updateCursor: 
    a = row.getValue("name of the "a" field name") 
    b = a # --> any manipulations to b can also go here.. example b = a + c etc... 
      # --> alternatively if you need to do string manipulations you can always make use of specifiers. such that a = "!%s!" %(a) note:use %d if "a" is a number. This would produce a string "!a!" if you needed it in that format. 
    row.setValue("name of "b" field", b) 
    updateCursor.update(row) 

以上將允許您獲取字段「A」的值,如果需要的話對這些值做一些處理並將其存儲爲b。然後用b填充字段「B」。

注意:如果字段B尚未創建,您需要這樣做...