Python - 坚持从csv读取特定行

时间:2012-10-08 02:27:06

标签: python csv arcpy

我需要根据csv将列添加到“匹配”的形状文件中。我有最后一步要完成,这是从csv获取值进入shp。

我得到了

  
    
      
        

readCSV [rowID] Traceback(最近一次调用last):TypeError中的文件“”,第1行:'_ csv.reader'         对象不可订阅

      
    
  

精简版CSV

enter image description here

文件看起来像 enter image description here

代码将OVL_CAT + OVL2_DESC填充到文件名。

然后我得到代码添加一个名为LGA_CODE的列,需要填充'583094',即第2行,第1列...当我无法调用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","#")

1 个答案:

答案 0 :(得分:1)

你的问题在于以下两行:

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

csv.reader是一个可迭代的文件行;它无法直接编入索引。您可以使用islice来获取所需的元素(islice(readCSV, rowID, rowID+1).next()),但更简洁的解决方案只是在您阅读时将rowID字典映射到AOI行它是第一次(在SearchStrings循环中):

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]