简体   繁体   English

在Sorting,Python之后填充唯一ID字段

[英]Populate Unique ID field after Sorting, Python

I am trying to create an new unique id field in an access table. 我正在尝试在访问表中创建一个新的唯一id字段。 I already have one field called SITE_ID_FD , but it is historical. 我已经有一个名为SITE_ID_FD字段,但它是历史字段。 The format of the unique value in that field isn't what our current format is, so I am creating a new field with the new format. 该字段中唯一值的格式不是我们当前格式的格式,因此我创建了一个具有新格式的新字段。

Old Format = M001, M002, K003, K004, S005, M006, etc

New format = 12001, 12002, 12003, 12004, 12005, 12006, etc

I wrote the following script: 我写了以下脚本:

fc = r"Z:\test.gdb\testfc"

x = 12001

cursor = arcpy.UpdateCursor(fc)

for row in cursor:
    row.setValue("SITE_ID", x)
    cursor.updateRow(row)
    x+= 1

This works fine, but it populates the new id field based on the default sorting of objectID. 这工作正常,但它根据objectID的默认排序填充新的id字段。 I need to sort 2 fields first and then populate the new id field based on that sorting (I want to sort by a field called SITE and then by the old id field SITE_ID_FD ) 我需要先排序2个字段,然后根据排序填充新的id字段(我想按名为SITE的字段排序,然后按旧的字段SITE_ID_FD

I tried manually sorting the 2 fields in hopes that Python would honor the sort, but it doesn't. 我试图手动排序2个字段,希望Python能够尊重排序,但事实并非如此。 I'm not sure how to do this in Python. 我不确定如何在Python中执行此操作。 Can anyone suggest a method? 谁能建议一种方法?

A possible solution is when you are creating your update cursor. 可能的解决方案是在创建更新游标时。 you can specify to the cursor the fields by which you wish it to be sorted (sorry for my english..), they explain this in the documentation: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000003m000000 你可以在光标上指定你希望它被排序的字段(对不起我的英文..),他们在文档中解释了这一点: http//help.arcgis.com/en/arcgisdesktop/10.0/help/的index.html#// 000v0000003m000000

so it goes like this: UpdateCursor(dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields}) and you are intrested only in the sort_fields so assuming that your code will work well on a sorted table and that you want the table ordered asscending the second part of your code should look like this: 所以它是这样的: UpdateCursor(dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})你只对sort_fields感兴趣所以假设你的代码在排序的表上运行良好并且你想要的排序的表格代表你的代码的第二部分应如下所示:

fc = r"Z:\test.gdb\testfc"

x = 12001

cursor = arcpy.UpdateCursor(fc,"","","","SITE A, SITE_ID_FD A") 
#if you want to sort it descending you need to write it with a D 
#>>  cursor = arcpy.UpdateCursor(fc,"","","","SITE D, SITE_ID_FD D")

for row in cursor:
    row.setValue("SITE_ID", x)
    cursor.updateRow(row)
    x+= 1

i hope this helps 我希望这有帮助

Added a link to the arcpy docs in a comment, but from what I can tell, this will create a new, sorted dataset-- 在评论中添加了arcpy文档的链接,但据我所知,这将创建一个新的,已排序的数据集 -

import arcpy
from arcpy import env

env.workspace = r"z:\test.gdb"

arcpy.Sort_management("testfc", "testfc_sort", [["SITE", "ASCENDING"],
                                                ["SITE_IF_FD", "ASCENDING]])

And this will, on the sorted dataset, do what you want: 这将在排序数据集上执行您想要的操作:

fc = r"Z:\test.gdb\testfc_sort"
x = 12001
cursor = arcpy.UpdateCursor(fc)

for row in cursor:
    row.setValue("SITE_ID", x)
    cursor.updateRow(row)
    x+= 1

I'm assuming there's some way to just copy the sorted/modified dataset back over the original, so it's all good? 我假设有一些方法可以将已排序/修改后的数据集复制回原始数据集,所以这一切都很好吗?

I'll admit, I don't use arcpy, and the docs could be a lot more explicit. 我承认,我不使用arcpy,而且文档可能更明确。

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

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