【问题标题】:Error: None-Type error during iteration with Python错误:使用 Python 进行迭代期间出现无类型错误
【发布时间】:2014-11-09 03:44:11
【问题描述】:

我创建了一个工作代码,用于将 GPX 文件转换为 ArcGIS 中的要素类。不幸的是,我遇到了一个损坏或加密的文件(我真的不知道)。我想为这些文件创建一个例外,因为可能还有更多,我不希望这些文件中断漫长的过程。我曾尝试使用 python 的 try 和 except 创建一个异常,但现在我在第 15 行得到错误“TypeError: 'NoneType' object is not iterable”,这是 FOR 循环。我的代码的简短说明:首先,我设置并命名要转换的文件。然后它们被转换,我已经包含了一个计数器,因为我正在转换数千个文件。转换后的文件被放入一个列表中,然后在合并中使用该列表在地理数据库中创建一个大要素类。对于无法转换的文件,我希望代码使用 arcpy.AddMessage() 为我提供文件名,然后转到其他文件。你有什么想法?这是我的代码:

import arcpy
import datetime
from arcpy import env
arcpy.env.overwriteOutput = True
gpxFolder =  'C:\file\with\GPXs\in\it'  
outputGdb = 'C:\the\output\geodatabase'  
mergedFile = 'C:the\output\geodatabase\mergedFile'    
env.workspace =gpxFolder



def convertGPX2feature(gpxFolder, outputGdb):  #, referenceSymbologyLayer, outputLayerFolder
    i = 1
    fcList = []

    for file in arcpy.ListFiles("*.gpx"):

        # Convert files from .gpx to feature layer
        # First set up the names
        inGPX = gpxFolder + "\\" + file
        featureName = file.partition(".gpx")[0]
        featurename2 = file.partition(".gpx")[1]
        fileType = featurename2.split(".")[1]
        outfile = outputGdb + "\\" + fileType + "_" + featureName

        try:
            # Now convert
            arcpy.GPXtoFeatures_conversion(inGPX,outfile)
            convertedfile = outfile
        except:
            arcpy.AddMessage("file " + featureName + " failed to convert")
            pass

        # Add a new field and populate it with the gpx file name to use for the join later
        arcpy.AddField_management(convertedfile, "Original_GPX_File", "DOUBLE", 9, "", "", "Original_GPX_File", "NULLABLE", "REQUIRED")
        arcpy.CalculateField_management(convertedfile, "Original_GPX_File", featureName)

        fcList.append(convertedfile)

        # The counter so you know where you are in the iteration
        if i%250 == 0:
            arcpy.AddMessage(str(i) + " files have been converted at " + str(datetime.datetime.now()))
        i += 1

    # Merge all of the converted files using fcList as the input
    arcpy.AddMessage("And now we merge")
    arcpy.Merge_management(fcList, mergedFile )


if __name__ == "__main__":
    convertGPX2feature(gpxFolder, outputGdb)  

【问题讨论】:

  • 添加异常本身会很有帮助
  • 也许arcpy.ListFiles 在找不到任何文件时返回Nonedocumentation 没有提到这一点,但坦率地说,我不相信文档说当它真正返回一个列表时它会返回一个字符串。
  • “添加异常本身”是什么意思,我该怎么做 user2085282? Kevin,在我添加 try 和 except 之前,代码运行良好,arcpy.ListFiles 函数运行良好。您认为尝试和例外是否以某种方式破坏了它?
  • 你使用convertedfile,即使它没有设置。我认为您的意思是 continue 而不是 pass (目前的 pass 无效),或者您的 try-catch 子句包含的代码太少。 BTW:featureName, featureName2 = file.partition('.gpx'),你可以在一个赋值中使用两个变量,这样可以避免冗余计算。
  • 凯文,你明白了。无论出于何种原因,ListFiles 都没有读取我的文件。我不确定我是如何修复它的,但它现在可以工作了。

标签: python file-conversion arcpy nonetype gpx


【解决方案1】:

所以问题是 arcpy.ListFiles() 突然停止工作。一旦我修复了这个问题(不知何故),我遇到了错误,即 GPX 功能已经具有新字段并且无法添加另一个同名字段,因为 convertfile 对象仍然保存损坏文件时最后传递的文件的信息。迭代过来。为了解决这个问题,我将 arcpy.AddField() 和 arcpy.CalculateField() 放在了 try 中。现在代码工作并在给我它无法使用 arcpy.AddMessage() 转换的消息后传递损坏的文件,并且只合并成功转换的文件。 新的 try and except 代码如下所示:

outfile = outputGdb + "\\" + fileType + "_" + featureName

try:
    # Now convert
    arcpy.GPXtoFeatures_conversion(inGPX,outfile)
    convertedfile = outfile

    # Add a new field and populate it with the gpx file name to use for the join later
    arcpy.AddField_management(convertedfile, "Original_GPX_File", "DOUBLE", 9, "", "", "Original_GPX_File", "NULLABLE", "REQUIRED")
    arcpy.CalculateField_management(convertedfile, "Original_GPX_File", featureName)
    fcList.append(convertedfile)

except:
    arcpy.AddMessage("File " + featureName + " could not be converted")

# The counter so you know where you are in the iteration
if i%250 == 0:
    arcpy.AddMessage(str(i) + " files have been converted at " + str(datetime.datetime.now()))
i += 1

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2017-05-28
    • 1970-01-01
    • 2020-04-18
    相关资源
    最近更新 更多