繁体   English   中英

Python-只能从CSV读取特定行

[英]Python - stuck at reading a specific row from a csv

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

我懂了

readCSV [rowID]追溯(最近一次呼叫最近):TypeError中的文件“”,第1行:“ _ csv.reader”对象不可下标

精简的CSV是

在此处输入图片说明

文件看起来像 在此处输入图片说明

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

然后我得到的代码添加一列名为LGA_CODE和需要“583094”,这是第2行1列...我怎么得到这个时候我不能把文件清单来填充它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","#")

您的问题在于以下两行:

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

csv.reader是文件行中的可迭代对象; 它不能直接索引。 您可以使用islice来获取所需的元素( islice(readCSV, rowID, rowID+1).next() ),尽管更rowID解决方案只是在第一次读取时将字典映射的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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM