错误:使用 Python 迭代期间无类型错误 [英] Error: None-Type error during iteration with Python

查看:71
本文介绍了错误:使用 Python 迭代期间无类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a working code to convert GPX files to feature classes in ArcGIS. Unfortunately I have ran into a file that is either corrupted or encrypted (I really don't know). I want to create an exception for these files because there may be more and I do not want these files interrupting the long process. I have tried to create an exception using python's try and except, but now I get the error "TypeError: 'NoneType' object is not iterable" for line 15 which is the FOR loop. A short explanation of my code: First I set up and make the names for the files to be converted. Then they are converted and I have included a counter because I am converting thousands of files. The converted files are put into a list which is then used in the merge to create one big feature class in a geodatabase. For the files that cannot be converted, I want the code to give me the name of the file using arcpy.AddMessage() and move on to the other files. Do you have any ideas? Here is my code:

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() 突然停止工作.一旦我解决了这个问题(以某种方式),我遇到了这样的错误,即 GPX 功能已经有了新的字段,并且无法添加另一个同名的字段,因为 convertfile 对象仍然保存了在损坏文件时最后通过的文件的信息迭代了一遍.为了解决这个问题,我将 arcpy.AddField() 和 arcpy.CalculateField() 放在了 try 中.现在代码工作并在给我它无法使用 arcpy.AddMessage() 转换并且只合并成功转换的文件的消息后传递损坏的文件.新的 try 和 except 代码如下所示:

So the problem was that arcpy.ListFiles() suddenly stopped working. Once I fixed that (somehow) I ran into the error that the GPX feature already had the new field and could not add another by the same name because the convertedfile object still held the information for the file that last passed when the corrupted file was being iterated over. To fix this, I put the arcpy.AddField() and arcpy.CalculateField() within the try. Now the code works and passes over the corrupted file after giving me the message that it failed to convert using arcpy.AddMessage() and only merges the successfully converted files. The new try and except code looks like this:

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

这篇关于错误:使用 Python 迭代期间无类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆