0

我需要将列添加到基于 csv 的“匹配”形状文件中。我还有最后一步要完成,即从 csv 获取要输入 shp 的值。

我明白了

readCSV[rowID] Traceback(最近一次调用最后一次):文件“”,第 1 行,类型错误:'_csv.reader' 对象不可下标

精简后的 CSV 是

在此处输入图像描述

文件看起来像 在此处输入图像描述

该代码将 OVL_CAT + OVL2_DESC 与文件名匹配。

然后,我获取代码以添加名为 LGA_CODE 的列,并需要使用第 2 行第 1 列的“583094”填充它...当我无法调用 FileList 2以从 csv 获取第 2 行时,我该如何获取(在下面的示例中为 3,但在 python 中为 2)?

import os, sys, datetime, csv, arcpy, string
from subprocess import Popen
from itertools import islice


top = os.getcwd() # change to a specific path if required.
# This otherwise starts with the directory the script is in (preferred).
RootOutput = r'P:\2012\273_CCRC_Townplanning_Datasets\Working\scratch' # change if you want output somewhere else
top=RootOutput
SourceDIR = r'P:\2012\273_CCRC_Townplanning_Datasets\Working\scratch\OM_011' # source of your data (subdirectories searched as well
outDIR = top+"\\workingFiles" # directory where output is written to. Includes temp files
finalDIR = top+"\\final" # folder for final data only
AOI = 'AOI.csv' # name of the file containing the las file names in the second column
Compare_Column = 2
Compare_Column2 = 3

# END setting base paths
# NOTHING BELOW should need editing.
FileTypes=['shp']
SearchStrings=[]
filecount=0
List =[]
count=0
x=0
os.chdir(top)

#Generate list with unique file name codes from CSV

FileList = csv.reader(open(AOI))
SearchStrings=[]
rowID=0
for File in FileList:
    #SearchStrings.append(File[0]+","+File[1])
    SearchStrings.append(str(rowID)+'_'+File[Compare_Column]+'_'+File[Compare_Column2])
    rowID=rowID+1

for root, dirs, files in os.walk(SourceDIR, topdown=False):
    for fl in files:
      currentFile=os.path.join(root, fl)
      for FileType in FileTypes:
          status= str.endswith(currentFile,FileType)
          if str(status) == 'True':
              List.append(currentFile)
              for SearchString in SearchStrings:
                  #print currentFile
                  #print SearchString
                  if str(SearchString in currentFile) == 'True':
                    #print str(currentFile)+str(status)
                    List.append(currentFile)
      filecount=filecount+1

#del fl

# Get list of Column Names
headers_count = 1

with open(AOI) as fin:
  headers = list(islice(fin, headers_count))
  delimiter=','
  header=str(headers)
  header_list=header.split(delimiter)


# Process matching files
for fl in List:
    header_count=0
    for header in header_list:
        dfStore=fl
        #arcpy.AddField_management(dfStore, str(header) ,'TEXT')

        # Get RowID to read column data from
        filename=fl[fl.rfind('\\')+1:fl.rfind('_')]
        for field in SearchStrings:
            #print field, filename
            if field.endswith(filename):
                rowID=field[:field.find('_')]
                with open(AOI, 'rb') as f:
                    readCSV= csv.reader(f)
                    text=readCSV[rowID][1]

##        arcpy.CalculateField_management(fl, header, text,"PYTHON_9.3")

=== 基于评论的更新代码 - 如果有人需要,一切都可以找到。

import os, sys, datetime, csv, arcpy, string
from subprocess import Popen
from itertools import islice


top = os.getcwd() # change to a specific path if required.
# This otherwise starts with the directory the script is in (preferred).
RootOutput = r'P:\2012\273_CCRC_Townplanning_Datasets\Working\scratch' # change if you want output somewhere else
top=RootOutput
SourceDIR = r'P:\2012\273_CCRC_Townplanning_Datasets\Working\scratch\OM_011' # source of your data (subdirectories searched as well
outDIR = top+"\\workingFiles" # directory where output is written to. Includes temp files
finalDIR = top+"\\final" # folder for final data only
AOI = 'AOI.csv' # name of the file containing the las file names in the second column
Compare_Column = 3
Compare_Column2 = 4

# END setting base paths
# NOTHING BELOW should need editing.
FileTypes=['shp']
SearchStrings=[]
filecount=0
List =[]
count=0
x=0
os.chdir(top)

#Generate list with unique file name codes from CSV

FileList = csv.reader(open(AOI))
SearchStrings=[]
rows=[]
#FinalList=[]
rowID=0
for File in FileList:
    #SearchStrings.append(File[0]+","+File[1])
    SearchStrings.append(str(rowID)+'_'+File[Compare_Column]+'_'+File[Compare_Column2])
    rows.append(File)
    #FinalList.append()
    rowID+=1

for root, dirs, files in os.walk(SourceDIR, topdown=False):
    for fl in files:
      currentFile=os.path.join(root, fl)
      for FileType in FileTypes:
          status= str.endswith(currentFile,FileType)
          if status:
              #List.append(currentFile)
              for SearchString in SearchStrings:
                  #print currentFile, SearchString
                  if str(SearchString[SearchString.find('_')+1:] in currentFile) == 'True':
                    #print str(currentFile)+str(status)
                    List.append(currentFile)
      filecount=filecount+1

#del fl

# Get list of Column Names
headers_count = 1

with open(AOI) as fin:
  headers = list(islice(fin, headers_count))
  delimiter=','
  header=str(headers)
  header_listT=header.split(delimiter)

header_list=[]

for hdr in header_listT:
    header_list.append(arcpy.ValidateTableName(hdr)[:10])

# Process matching files

columnID=1

for fl in List:
    header_count=0
    for header in header_list:
        print header
        dfStore=fl
        try:
            arcpy.AddField_management(dfStore, str(header) ,'TEXT')
        except:
            pass

        # Get RowID to read column data from
        filename=fl[fl.rfind('\\')+1:fl.rfind('_')]

        for field in SearchStrings:
        #print field, filename
            #print header, field
            if field.endswith(filename):
                #print 'FOUND......................'
                column_count=len(fl)
                if columnID < len(header_list):
                    rowID=int(field[:field.find('_')])
                    text = rows[rowID][columnID]
                    print filename, header, text
                    columnID+=1
                    arcpy.CalculateField_management(fl, header, "text" ,"PYTHON_9.3")

#arcpy.CalculateField_management("P:/2012/273_CCRC_Townplanning_Datasets/Working/scratch/OM_011/OM_011_Waterway_Envelopes_ccrc.shp","LGA_CODE","5","PYTHON","#")
4

1 回答 1

1

您的问题在于这两行:

readCSV= csv.reader(f)
text=readCSV[rowID][1]

csv.reader是对文件行的可迭代;它不能被直接索引。您可以使用islice来获取您想要的元素 ( ),但更简洁的解决方案是在您第一次阅读时islice(readCSV, rowID, rowID+1).next()将字典映射存储rowID到该行(在循环中):AOISearchStrings

FileList = csv.reader(open(AOI))
SearchStrings = []
rows = []
rowID=0
for File in FileList:
    #SearchStrings.append(File[0]+","+File[1])
    SearchStrings.append(str(rowID)+'_'+File[Compare_Column]+'_'+File[Compare_Column2])
    rows.append(File)
    rowID=rowID+1

... # later

rowID=int(field[:field.find('_')])
text = rows[rowID][1]
于 2012-10-08T02:39:31.617 回答