简体   繁体   中英

Python Skipping my for loop

First off note that I am a beginner in Python. Taking a class currently dealing with Python in an ArcGIS environment. My current project is a simple program to create files and copy other files to them. However part of the assignment is to have print statements state what is going on as it happens, for instance the final print statement should look like:

Processing Polygon FeatureClasses....

Processing FeatureClass >> Building

Field Information:

 etc.

Here is the small bit of my code that should do that:

pointlist = arcpy.ListFeatureClasses("*", "Point")
print "Processing Point FeatureClasses..."
for pl in pointlist:
    arcpy.MakeFeatureLayer_management(pl, "Point" + 1)
    pointlayer = arcpy.SelectLayerByLocation_management(pl, "intersect", MapGridID)
    pointcount = int(arcpy.GetCount_management(pointlayer).getOutput(0))
    if pointcount >= 1:
        arcpy.CopyFeatures_management(pointlayer, OutputGDB)
    for pl in pointlist:
        print "Processing FeatureClass:" + pl
        pointfield = arcpy.ListFields()
        for pf in pointfield:
            print "Field Name:" + pf

The issue arises that it prints the first print statement, "Processing Point FeatureClasses" but doesn't do the rest and then skips to my next part of the code and executes that. Any idea why? Sorry if my formatting or wording is off/sounds weird. Thank you.

EDIT

I emailed my professor as well asking for some guidance and he wrote back a slightly edited version of my above code block. I can now get everything to print out except the pointfield print statements, those are now getting skipped. Here is the code:

pointlist = arcpy.ListFeatureClasses("*", "Point")
print "Processing Point FeatureClasses...\n"
i = 1
for pl in pointlist:
    print "Processing FeatureClass: " + pl
    featlayernamepoint = "Point" + str(i)
    arcpy.MakeFeatureLayer_management(pl, featlayernamepoint)
    arcpy.SelectLayerByLocation_management(featlayernamepoint, "intersect",   featurelayerMG2)
    pointcount = int(arcpy.GetCount_management(featlayernamepoint).getOutput(0))
    if pointcount >= 1:
        arcpy.CopyFeatures_management(featlayernamepoint, OutputGDB)
        pointfield = arcpy.ListFields(featlayernamepoint)
        for pf in pointfield:
            print "Field Name: " + pf.name
    i += 1

You forgot to pass the point to ListFields()

pointfield = arcpy.ListFields(pl)
for pf in pointfield:
      print "Field Name:" + pf.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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