Python在一些迭代之后停止循环遍历列表

2024-10-03 21:33:21 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的工作中,我不得不编写一个python脚本来生成几个显示不同学校相似数据的地图。我用一个光标在我的列表中循环,这实际上对我列表中的前28所学校非常有效。但是,当我将下一张地图导出到pdf文件时,我收到了一条错误消息。在

AttributeError: PageLayoutObject: Error in executing ExportToPDF

我还试图将地图只导出到一个jpeg文件,这个文件的工作时间稍长,但也会在大约50次迭代后停止,并出现相同的错误。在

我不知道为什么我的脚本在前28次迭代中工作,然后停止。有人有主意吗?可能是Python不能将这么多文件保存到我要指向的文件夹吗?或者是你的记忆被填满了?我该怎么办?在

我对Python编程还是个新手,所以我不知道如何解决这个问题。在

以下是我的部分代码(可能会有所帮助):

从初始化光标开始:

Schulliste = "filepath/Schulliste_PrSt.csv"
fields = ('Schule_Nr', 'Schule_Name') 
CursorSchule = arcpy.da.SearchCursor(Schulliste, fields) 
for row in CursorSchule:
*[a lot of tasks to set up the map]* 

将地图保存为pdf和jpeg文件的代码结束:

^{pr2}$

循环的总代码:

for row in CursorSchule:
    #define mapdoc and dataframes
    mxd = "filepath/Standard_Einzug.mxd"
    mapdoc = arcpy.mapping.MapDocument(mxd)
    df = arcpy.mapping.ListDataFrames(mapdoc)[0]
    df_Sus = arcpy.mapping.ListDataFrames(mapdoc)[1]


    #define new layers
    lyrSchule = arcpy.mapping.Layer("filepath/FHH_Schulen_SJST_2014_SingleSym_red.lyr")
    lyrNachbarsch = arcpy.mapping.Layer("filepath/FHH_Schulen_SJST_2014_SingleSym_blue.lyr")
    lyrEinzug = arcpy.mapping.Layer("filepath/FHH_StatGeb_2011_GradCol.lyr")

    #define legends
    legend1 = arcpy.mapping.ListLayoutElements(mapdoc, "LEGEND_ELEMENT", "Legende1")[0]
    legend2 = arcpy.mapping.ListLayoutElements(mapdoc, "LEGEND_ELEMENT", "Legende2")[0]

    #add table for every School with table join 
    folder = "filepath/"
    joinTable = join(folder, "Daten_{0}.csv".format(row[0]))
    arcpy.AddJoin_management(lyrEinzug, "StatGeb_Nr", joinTable, "StatGeb_Nr")
    arcpy.FeatureClassToShapefile_conversion(lyrEinzug, "filepath")

    #add saved shapefile
    lyrEinzug = arcpy.mapping.Layer("GPL0.shp")
    legend1.autoAdd = False
    legend2.autoAdd = True
    arcpy.mapping.AddLayer(df, lyrEinzug)
    for lyr in arcpy.mapping.ListLayers(mapdoc):
        if lyr.name == "GPL0":
            lyr.name = "Gebietsbezogen"

    #Change symbology of the layer to graduated Color
    lyrEinzug = arcpy.mapping.ListLayers(mapdoc, "Gebietsbezogen")[0]
    sourceLayer = arcpy.mapping.Layer("filepath/StatGeb_Label_Halo_Symbol.lyr")
    arcpy.mapping.UpdateLayer(df, lyrEinzug, sourceLayer, False)
    arcpy.RefreshActiveView()
    lyrEinzug = arcpy.mapping.ListLayers(mapdoc, "Gebietsbezogen")[0]

    #Change symbology classes
    fieldlist = arcpy.ListFields("filepath/GPL0.shp")
    valueField = fieldlist[-1]
    lyrEinzug.symbology.valueField = valueField.name
    lyrEinzug.symbology.classBreakValues = [0, 5, 10, 20, 40, 70, 100]
    lyrEinzug.symbology.classBreakLabels = ["unter 5% der SuS", "5 bis unter 10% der SuS", "10 bis unter 20% der SuS", "20 bis unter 40% der SuS", "40 bis unter 70% der SuS", "über 70% der SuS"]
    lyrEinzug.transparency = 50

    #Data Frame Extent
    queryField = fieldlist[7]
    fieldname = queryField.name
    delimfield = arcpy.AddFieldDelimiters(lyrEinzug, fieldname)
    attribute_query = delimfield + " = {0}".format(row[0])
    lyrEinzug.definitionQuery = attribute_query
    df.extent = lyrEinzug.getSelectedExtent(False)
    arcpy.RefreshActiveView()

    #add labels
    labelField = fieldlist[9]
    if lyrEinzug.supports("LABELCLASSES"):
        for lblclass in lyrEinzug.labelClasses:
            lblclass.showClassLabels = True
    lblclass.expression = "\"<FNT name='Arial' size='3'><CLR green = '112' blue = '255'>\"" + " & [" + labelField.name + "] & " + '"</CLR></FNT>"'
    lyrEinzug.showLabels = True
    arcpy.RefreshActiveView()

    #select specific school
    fieldSchule = "Schule_Nr"
    delimfieldSchule = arcpy.AddFieldDelimiters(lyrSchule, fieldSchule)
    querySchule = delimfieldSchule + " = {0}".format(row[0])
    lyrSchule.definitionQuery = querySchule
    arcpy.FeatureClassToShapefile_conversion(lyrSchule, "filepath/02_GIS-Daten")
    lyrSchule = arcpy.mapping.Layer("GPL0_1.shp")
    legend1.autoAdd = True
    legend2.autoAdd = False
    arcpy.mapping.AddLayer(df, lyrSchule, "TOP")
    lyrSchule = arcpy.mapping.ListLayers(mapdoc, "GPL*")[0]
    sourceLayer = arcpy.mapping.Layer("filepath/Schule_Labels_Halo.lyr")
    arcpy.mapping.UpdateLayer(df, lyrSchule, sourceLayer, False)
    lyrSchule.name = "{0} ({1})".format(row[1], row[0])

    if lyrSchule.supports("LABELCLASSES"):
        for lblclass in lyrSchule.labelClasses:
            lblclass.showClassLabels = True
            lblclass.expression = '"%s" & [Schule_Nam] & "%s"' % ("<BOL><FNT name='Arial' size='3'><CLR red = '255'>", "</CLR></FNT></BOL>")
    lyrSchule.showLabels = True
    arcpy.RefreshActiveView()

    #select neighboring Schools within 1.5 km
    arcpy.Buffer_analysis("filepath/buffer", 1500)
    lyrBuffer = arcpy.mapping.Layer("buffer.shp")
    arcpy.SelectLayerByLocation_management(lyrNachbarsch, "WITHIN", lyrBuffer)
    arcpy.FeatureClassToShapefile_conversion(lyrNachbarsch, "filepath")
    lyrNachbarsch = arcpy.mapping.Layer("filepath/GPL0_2.shp")
    legend1.autoAdd = True
    legend2.autoAdd = False
    arcpy.mapping.InsertLayer(df, lyrSchule, lyrNachbarsch, "AFTER")
    for lyr in arcpy.mapping.ListLayers(mapdoc):
        if lyr.name == "GPL0_2":
            lyr.name = "Nachbarschulen (<1,5km)"
    lyrNachbarsch = arcpy.mapping.ListLayers(mapdoc, "Nachbar*")[0]
    sourceLayer = arcpy.mapping.Layer("filepath/FHH_Schulen_SJST_2014_SingleSym_blue.lyr")
    arcpy.mapping.UpdateLayer(df, lyrNachbarsch, sourceLayer, True)

    #if selected School is elementary School, also only select neighbouring elementary Schools
    SC = arcpy.SearchCursor(lyrSchule)
    for Schule in SC:
        SF = Schule.getValue("Schule_SF")
    fieldform = "Schule_SF"
    delimfield1 = arcpy.AddFieldDelimiters(lyrNachbarsch, fieldform)
    query_SF = delimfield1 + "= 'G'"
    lyrNachbarsch.definitionQuery = query_SF
    arcpy.RefreshActiveView()

    del SC, Schule

    #Labels for schools
    fieldname = "Schule_Nr"
    delimfield2 = arcpy.AddFieldDelimiters(lyrNachbarsch, fieldname)
    query_Nachbar = delimfield2 + "<> {0}".format(row[0])
    if lyrNachbarsch.supports("LABELCLASSES"):
        for lblclass in lyrNachbarsch.labelClasses:
            lblclass.className = "Schulname"
            lblclass.SQLQuery = query_Nachbar
            lblclass.expression = '"%s" & [Schule_Nam] & "%s"' % ("<FNT name='Arial' size='3'><CLR red = '100' blue = '100' green = '100'>", "</CLR></FNT>")
            lblclass.showClassLabels = True
    lyrNachbarsch.showLabels = True
    arcpy.RefreshActiveView()

    #Change text
    #Titel
    Titel = arcpy.mapping.ListLayoutElements(mapdoc, "TEXT_ELEMENT", "Titel")[0]
    Titel.text = "Einzugsgebiet Jahrgänge 1-4"

    #Legende
    style_StatGeb = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "Einzug")[0]
    style_Schulen = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "Schule")[0]
    legend1.updateItem(lyrSchule, style_Schulen)
    legend1.updateItem(lyrNachbarsch, style_Schulen)
    legend2.updateItem(lyrEinzug, style_StatGeb)
    legend1.elementPositionY = 25
    legend2.elementPositionY = 11.0
    legend2.elementPositionX = 1.5

    #number of pupils
    lyrSchuldaten = arcpy.mapping.Layer("filepath/Schulliste_PrSt.lyr")
    arcpy.mapping.AddLayer(df_Sus, lyrSchuldaten)
    lyrSchuldaten = arcpy.mapping.ListLayers(mapdoc, "Schulliste*")[0]
    querySchule = delimfield2 + "= {0}".format(row[0])
    lyrSchuldaten.definitionQuery = querySchule
    df_Sus.extent = lyrSchuldaten.getSelectedExtent(False)
    arcpy.RefreshActiveView()

    #Infotext
    Infotext = arcpy.mapping.ListLayoutElements(mapdoc, "TEXT_ELEMENT", "Infotext")[0]
    Infotext.text = "nicht darstellbar: SuS aus dem Umland \n \nAggregation: Statistische Gebiete \nKlassenberechnung: manuell gesetzte Intervalle \nDatenbezug: Wohnort der SuS \nDatenauszug: 10.01.14 \nKartengrundlage: DISK60"
    Infotext.elementPositionY = 1.73

    #Export map
    if SF == "STS":
        arcpy.mapping.ExportToPDF(mapdoc, "filepath/Einzugsgebiet_{0}.pdf".format(row[0]))
        arcpy.mapping.ExportToJPEG(mapdoc, "filepath/Einzugsgebiet_{0}.jpg".format(row[0]))
    elif SF == "G":
        arcpy.mapping.ExportToPDF(mapdoc, "filepath/Einzugsgebiet_{0}.pdf".format(row[0]))
        arcpy.mapping.ExportToJPEG(mapdoc, "filepath/Einzugsgebiet_{0}.jpg".format(row[0]))


    #delete shapefiles
    arcpy.Delete_management("GPL0.shp")
    arcpy.Delete_management("GPL0_1.shp")
    arcpy.Delete_management("GPL0_2.shp")

编辑:

今天,我试图用更高的解决方案保存jpeg文件,这导致错误消息比以前早了很多,这可能会强调它与可用内存有关?在


Tags: namelayerdfformappingrowarcpyfilepath
1条回答
网友
1楼 · 发布于 2024-10-03 21:33:21

试着摆弄你的循环从第28张地图上得到的变量。看起来你在使用一个csv文件来完成这一切,也许可以将一个工作映射的列复制到不工作的行中,只是为了确认这是否是内存问题。一旦你确认了这一点,请仔细检查你第28所学校的所有数值。在尝试转换为pdf时,其中一个可能无效。在

相关问题 更多 >