简体   繁体   English

Python跳过我的for循环

[英]Python Skipping my for loop

First off note that I am a beginner in Python. 首先请注意,我是Python的初学者。 Taking a class currently dealing with Python in an ArcGIS environment. 上一个当前在ArcGIS环境中处理Python的类。 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. 出现的问题是,它打印第一个打印语句“ Processing Point FeatureClasses”,但不执行其余操作,然后跳到代码的下一部分并执行。 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() 您忘记了将点传递给ListFields()

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

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

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