Select to view content in your preferred language

How to sort a field using a wildcard for its name

5931
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
1 Solution

Accepted Solutions
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.

View solution in original post

17 Replies
SepheFox
Deactivated User

I'm not really clear on what you're trying to do. Do you want the fid in the same order as the other field?​

KONPETROV
Frequent Contributor

No, i don't want to change the FID. In some of my shp the column "P500" is ascended starting from 0 but in some is descended starting from for example 20. I want to sort ascend all my shp based in their fields which start from "P500". The main problem is that the name of that field in each shp is different after the first 4 characters (for example P500r21 or PM500_20EF) that's why i am trying to use a wildcard to do it.

0 Kudos
SepheFox
Deactivated User

I believe you would just use the field name"P500*". That's a star to donate the wildcard.

0 Kudos
KONPETROV
Frequent Contributor

Sephe i run it in Python window but i get an

Runtime error

Traceback (most recent call last):

  File "<string>", line 5, in <module>

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

    return gp.listFields(dataset, wild_card, field_type)

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

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

IOError: "#" does not

exist

0 Kudos
XanderBakker
Esri Esteemed Contributor

I assume you want to loop through all the "samp*" shapefiles in the given path and for each "P500*" field sort it and store the result as a new featureclass.

If this is true, you could use something like this:

import arcpy, os
arcpy.env.workspace = "c:/Bo/Well/Point"
fcs = arcpy.ListFeatureClasses("samp*", "point")
for fc in fcs:
    fields = arcpy.ListFields(fcs, "PNT500*", "Double")
    for fld in fields:
        fc_sort = "{0}_{1}.shp".format(os.path.splitext(fc)[0], fld.name)
        arcpy.Sort_management(fc, fc_sort, [[fld.name, "ASCENDING"]])

Observations:

  • eliminated the "from arcpy import env" since you don't use it
  • list the fields for each featureclass (inside the loop through the featureclasses
  • In the example your fields start with PNT500, not P500
  • you should provide the output name fc_sort, in this case it creates a shapefile based on the input featureclass name and the field used to sort it
  • the field for sorting it the name of the field, not a field object, hence "fld.name"
KONPETROV
Frequent Contributor

Mr Bakker thanks a lot for your response. I tried to make a tool with your scipt because i want to run it inside modelbuilder but it fails. Here is the code. I changed the "samp" and "PNT500" cause i run the model again and i have different names now. But i cannot see where the problem is

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

I get that error

Traceback (most recent call last):

File "C:\Users\Kostas\Desktop\Xander.py", line 5, in <module>

fields = arcpy.ListFields(fcs, "P10*", "Double")

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

return gp.listFields(dataset, wild_card, field_type)

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

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

IOError: "[u'evsamp10c10.shp', u'evsamp10c20.shp', u'evsamp10c30.shp', u'evsamp10c40.shp', u'evsamp10c50.shp', u'evsamp10c60.shp', u'evsamp10cb.shp', u'evsamp10cm10.shp', u'evsamp10cm20.shp', u'evsamp10cm30.shp', u'evsamp10cm40.shp', u'evsamp10cm50.shp', u'evsamp10cm60.shp', u'evsort10.shp']" does not exist

Failed to execute

0 Kudos
XanderBakker
Esri Esteemed Contributor

Oops, I see the error. The ListFields takes the featureclass as input not the list of featureclasses. Change "fcs" to "fc".

KONPETROV
Frequent Contributor

I changed it like that but nothing changed

import arcpy, os
env.workspace = arcpy.GetParameterAsText(0)
fcs = arcpy.ListFeatureClasses("ev*", "point")
for fc in fcs:
    fields = arcpy.ListFields(fc, "P10*", "Double")
    for fld in fields:
        fc_sort = "{0}_{1}.shp".format(os.path.splitext(fc)[0], fld.name)
        arcpy.Sort_management(fc, fc_sort, [[fld.name, "ASCENDING"]])
0 Kudos
XanderBakker
Esri Esteemed Contributor

I think line 2 should also yield an error, since you are not importing env from arcpy and set the workspace directly to env.workspace. Either add the line from arcpy import env or change line 2 to arcpy.env.workspace = ...

You state that nothing changed, but the same error cannot occur.

The list of featureclasses "fcs" is [u'evsamp10c10.shp', u'evsamp10c20.shp', u'evsamp10c30.shp', u'evsamp10c40.shp', u'evsamp10c50.shp', u'evsamp10c60.shp', u'evsamp10cb.shp', u'evsamp10cm10.shp', u'evsamp10cm20.shp', u'evsamp10cm30.shp', u'evsamp10cm40.shp', u'evsamp10cm50.shp', u'evsamp10cm60.shp', u'evsort10.shp']. You loop through this list and for each fc (like 'evsamp10c10.shp') you list the fields. What is the exact error?

If you are willing to attach some sample files I can check if it works on my computer...

0 Kudos