使用csv和函数构造循环

时间:2016-01-20 04:25:04

标签: python-2.7 loops csv

我在循环中遇到循环问题。 我希望对于每个要素类(FC),脚本应该遍历" checklist"并且匹配的地方运行函数findfield。 目前该脚本似乎适用于第一个功能类,遍历清单,但它只是列出剩余的功能类而不检查清单。任何想法?建议

提前谢谢

# Import system modules
import arcpy
import os
import traceback
from arcpy import env
import csv
import sys

#Workspace
arcpy.env.workspace = "SchemaOnly.gdb"

usecsv = open(r'C:\Temp\GV\Final working\Domain and length out.csv','r') #open file
checklist = csv.reader(usecsv) #init csv reader

# function to change schema of featureclass
def FindField(fc,myField):
    fieldList = arcpy.ListFields(fc)

    for field in fieldList:
        if str.lower(str(field.name)) == str.lower(myField):
            arcpy.AlterField_management(fc, myField, "", "", "text", new_length, "", "")
            print "    " + fc + " contains fieldname: " + myField



#Search Feature Datasets
for fds in arcpy.ListDatasets('','feature'):
    print "Searching FeatureDataset: " + fds

    for fc in arcpy.ListFeatureClasses('','',fds):
        print "  Searching Featureclass... " + fc
        for row in checklist:
            #print row[0] #prints fieldname
            #print row[1] #prints newlength
            myField = row[0]
            new_length=row[1]
            FindField(fc,myField)

1 个答案:

答案 0 :(得分:0)

好的,所以我设法确定了自己的修复方法。问题似乎是当我想要遍历清单时,它在嵌套循环中工作了一次但不是再次。  解决方案是将清单附加到新列表(fieldInfo)中,然后遍历该列表。代码更改

checklist = csv.reader(usecsv) #init csv reader
fieldInfo = []
for row in checklist:
    fieldInfo.append([row[0],row[1]])