Select to view content in your preferred language

How to sort a field using a wildcard for its name

5933
17
Jump to solution
05-24-2015 09:22 AM
KONPETROV
Frequent Contributor

Hello everyone, i have a problem with my point shapefiles in which i implement a big procession. The problem is that although all the shapefiles have the same columns there is one which starts with "P500" AND continues differently to each one which in some it is sort ascended and in others sort descended. Is there a way with python where i can sort ascend this column for every point file by using "P500" as wildcard. I tried it but my FID changed also causing me problems.

In some shp i have this   and in some other this(problem)      and i want it to be like the first two columns

FID     PNT500B                    FID     PNT500C20            

0          0                                   0          3

1          1                                   1          2

2          2                                   2          1

3          3                                   3          0

This is the code i have written but it doesn't work properly

import arcpy
from arcpy import env
arcpy.env.workspace = "c:/Bo/Well/Point"
fcs = arcpy.ListFeatureClasses("samp*", "point")
fields = arcpy.ListFields(fcs, "P500*", "Double")
for fc in fcs:
   for fiel in fields:
  arcpy.Sort_management(fc, fc_sort, [[fiel, "ASCENDING"]])
Tags (3)
0 Kudos
17 Replies
XanderBakker
Esri Esteemed Contributor

I created two shapefiles starting with "ev" and added some fields starting with "P10" and ran this code:

import arcpy, os
# env.workspace = arcpy.GetParameterAsText(0)
arcpy.env.workspace = r"C:\Forum\SortFields"
fcs = arcpy.ListFeatureClasses("ev*", "point")
print fcs
for fc in fcs:
    print fc
    fields = arcpy.ListFields(fc, "P10*", "Double")
    for fld in fields:
        print fld.name
        fc_sort = "{0}_{1}.shp".format(os.path.splitext(fc)[0], fld.name)
        print fc_sort
        arcpy.Sort_management(fc, fc_sort, [[fld.name, "ASCENDING"]])

The output printed is:

[u'evsamp10c10.shp', u'evsamp10cm50.shp']
evsamp10c10.shp
P10bla
evsamp10c10_P10bla.shp
evsamp10cm50.shp
P10a
evsamp10cm50_P10a.shp
P10b
evsamp10cm50_P10b.shp

The output shapefile are sorted on the field provided...

0 Kudos
KONPETROV
Frequent Contributor

i tried it again but the data in shapefiles remain unsorted.. If you could try with these 6 shp would be great !!!

0 Kudos
XanderBakker
Esri Esteemed Contributor

I just ran the code and at first there was no result (no error either). When I looked at the data I noticed that the P10 fields are not double, but long. So if you use "Integer" in ListFields you will get the result.

Code used:

import arcpy, os
# env.workspace = arcpy.GetParameterAsText(0)
arcpy.env.workspace = r"D:\Xander\GeoNet\SortFields"
fcs = arcpy.ListFeatureClasses("ev*", "point")

# define a output folder, to store the results
out_fldr = r"D:\Xander\GeoNet\SortFields\result"

print fcs
for fc in fcs:
    print fc
    fields = arcpy.ListFields(fc, "P10*", "Integer")
    print fields
    for fld in fields:
        print " - {0}".format(fld.name)
        fc_sort = os.path.join(out_fldr, "{0}_{1}.shp".format(os.path.splitext(fc)[0], fld.name))
        print " - {0}".format(fc_sort)
        arcpy.Sort_management(fc, fc_sort, [[fld.name, "ASCENDING"]])

Result printed:

[u'evsamp10c10.shp', u'evsamp10c20.shp', u'evsamp10c30.shp', u'evsamp10cm10.shp', u'evsamp10cm20.shp', u'evsamp10cm30.shp']
evsamp10c10.shp
[<Field object at 0x2301df0[0x22cecb0]>]
 - P10C10
 - D:\Xander\GeoNet\SortFields\result\evsamp10c10_P10C10.shp
evsamp10c20.shp
[<Field object at 0x23016b0[0x22cede8]>]
 - P10C20
 - D:\Xander\GeoNet\SortFields\result\evsamp10c20_P10C20.shp
evsamp10c30.shp
[<Field object at 0x23695f0[0x22ce338]>]
 - P10C30
 - D:\Xander\GeoNet\SortFields\result\evsamp10c30_P10C30.shp
evsamp10cm10.shp
[<Field object at 0x2369790[0x22ced10]>]
 - P10CM10
 - D:\Xander\GeoNet\SortFields\result\evsamp10cm10_P10CM10.shp
evsamp10cm20.shp
[<Field object at 0x2369190[0x22cede8]>]
 - P10CM20
 - D:\Xander\GeoNet\SortFields\result\evsamp10cm20_P10CM20.shp
evsamp10cm30.shp
[<Field object at 0x23695f0[0x22ce338]>]
 - P10CM30
 - D:\Xander\GeoNet\SortFields\result\evsamp10cm30_P10CM30.shp

See attached results.

KONPETROV
Frequent Contributor

Mr Bakker thanks a lot for your time. I ve written this but still doesn't work, either it succeded without results or it raised an error. Is there any difference if the folder i have my shp is named points or Points or Kos etc? Is there any problem with the folder i choose to save the new fc? This is the code for the tool:

import arcpy
import os
from arcpy import env
arcpy.overwriteOutput = True
inWorkspace = arcpy.GetParameterAsText(0)
fcs = arcpy.ListFeatureClasses("ev*", inWorkspace)    
# define a output folder, to store the results  
out_fldr = arcpy.GetParameterAsText(1) 
for fc in fcs:  
    fields = arcpy.ListFields(fc, "P10*", "Integer")  
    for fld in fields:  
        fc_sort = os.path.join(out_fldr, "{0}_{1}.shp".format(os.path.splitext(fc)[0], fld.name))  
        arcpy.Sort_management(fc, fc_sort, [[fld.name, "ASCENDING"]]) 

And these are the parameters i defined, i also tried to put outfolder as workspace

Parameters.png

but i still get that error:

Traceback (most recent call last):

  File "C:\Users\Kos\Desktop\scriptsort.py", line 6, in <module>

    fcs = arcpy.ListFeatureClasses("ev*", inWorkspace)

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\__init__.py", line 896, in ListFeatureClasses

    return gp.listFeatureClasses(wild_card, feature_type, feature_dataset)

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 314, in listFeatureClasses

    self._gp.ListFeatureClasses(*gp_fixargs(args, True)))

RuntimeError: ERROR 999999: Error executing function.

Feature type is invalid

0 Kudos
XanderBakker
Esri Esteemed Contributor

The error "Feature type is invalid" refers to the "ListFeatureClasses(wild_card, feature_type, feature_dataset)" function.

Change your code into this:

import arcpy
import os
inWorkspace = arcpy.GetParameterAsText(0)

arcpy.env.overwriteOutput = True
arcpy.env.workspace = inWorkspace

fcs = arcpy.ListFeatureClasses("ev*", "point")
# define a output folder, to store the results
out_fldr = arcpy.GetParameterAsText(1)
for fc in fcs:
    fields = arcpy.ListFields(fc, "P10*", "Integer")
    for fld in fields:
        fc_sort = os.path.join(out_fldr, "{0}_{1}.shp".format(os.path.splitext(fc)[0], fld.name))
        arcpy.Sort_management(fc, fc_sort, [[fld.name, "ASCENDING"]])

When you define the parameters of the Toolbox script, you should define the direction of the output folder as input. Although your (output) results will be written to this folder, it is actually an input variable for the tool.

KONPETROV
Frequent Contributor

You are the One. The Guru of python. Thanks a lot for your time Mr. Bakker. It works perfectly at last!!! (i could have never thought of that with the "output")

0 Kudos
XanderBakker
Esri Esteemed Contributor

Glad it works for you. Many of us have struggled with the "output" being of direction input...

KONPETROV
Frequent Contributor

Mr Bakker one last tricky question. I have a problem with the code above due to that reason (output) at modelbuilder, because i don't have really an output to use as a precondition to an another tool. How can i make an output for this tool?

0 Kudos