0

csvに基づいて「一致した」シェープファイルに列を追加する必要があります。完了する最後のステップが1つあります。それは、csvからshpに入力する値を取得することです。

私は得る

readCSV [rowID]トレースバック(最後の最後の呼び出し):ファイル ""、1行目、TypeError:'_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

あなたの問題はこれらの2行にあります:

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

csv.readerファイルの行を反復可能です。直接インデックスを作成することはできません。islice必要な要素()を取得するために使用できますが、より適切な解決策は、最初に(ループ内で)読み取るときに行へislice(readCSV, rowID, rowID+1).next()の辞書マッピングを格納することです。rowIDAOISearchStrings

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 に答える