Extract value from expression

521
2
Jump to solution
11-18-2021 12:41 PM
StanislavaV
Occasional Contributor

Hi,

I have a SHP with these attributes:

ID1NAME1AREAID2NAME2
123John0,12135Lisa
123John1246Veronica
456Kyra3,2246Veronica
789Oliver4,5123John
012Nick78123John
012Nick8,9100Nelson

 

Then I need to make an expression based on ID´s - for example for JOHN.

"ID1=123 or ID2=123"

 But then I need to export it as a new SHP but I want it with ID in output name.

So output file would be "Polygons_123"

For another file would be "Polygons_789" and so on....

 

Is there any option how to extract this value from expression and make it through Calculate value maybe? Or is there another way?

thank you.

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Edit and run this code in the python window.

input_path = "path:/to/your/SHP.shp"
output_path = "path:/to/output/folder_or_gdb"

# get unique values for id1 and id2
raw_ids = [r for r in arcpy.da.SearchCursor(input_path, ["ID1", "ID2"])]
ids = [ri[0] for ri in raw_ids] + [ri[1] for ri in raw_ids]
unique_ids = list(set(ids))

# loop through unique_ids
for uid in unique_ids:
    where_clause = "ID1 = {0} OR ID2 = {0}".format(uid)
    output_name = "Polygons_{}".format(uid)
    arcpy.conversion.FeatureClassToFeatureClass(input_path, output_path, output_name, where_clause)

Have a great day!
Johannes

View solution in original post

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

don't know if it was ported back to arcmap, but

Split By Attributes (Analysis)—ArcGIS Pro | Documentation

then there may be the issue about output file names.


... sort of retired...
0 Kudos
JohannesLindner
MVP Frequent Contributor

Edit and run this code in the python window.

input_path = "path:/to/your/SHP.shp"
output_path = "path:/to/output/folder_or_gdb"

# get unique values for id1 and id2
raw_ids = [r for r in arcpy.da.SearchCursor(input_path, ["ID1", "ID2"])]
ids = [ri[0] for ri in raw_ids] + [ri[1] for ri in raw_ids]
unique_ids = list(set(ids))

# loop through unique_ids
for uid in unique_ids:
    where_clause = "ID1 = {0} OR ID2 = {0}".format(uid)
    output_name = "Polygons_{}".format(uid)
    arcpy.conversion.FeatureClassToFeatureClass(input_path, output_path, output_name, where_clause)

Have a great day!
Johannes
0 Kudos