Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:56:59+00:00 2026-06-12T12:56:59+00:00

I need to add columns to a matched shape file based on a csv.

  • 0

I need to add columns to a “matched” shape file based on a csv. I have one last step to complete which is to get the value to enter into the shp from the csv.

I get

readCSV[rowID] Traceback (most recent call last): File “”, line 1, in TypeError: ‘_csv.reader’
object is not subscriptable

The stripped down CSV is

enter image description here

The files look like
enter image description here

The code mataches OVL_CAT + OVL2_DESC to the File Name.

I then get the code to add a column called LGA_CODE and need to populate it with ‘583094’ which is row 2, column 1…how do I get this when I can’t call FileList2 to get row 2 from the csv (3 in the example below but 2 in python)?

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")

=== UPDATED CODE BASED ON COMMENTS -it’s all working find if anyone needs it.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:57:00+00:00Added an answer on June 12, 2026 at 12:57 pm

    Your problem is in these two lines:

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

    csv.reader is an iterable over the lines of the file; it cannot be directly indexed. You could use islice to get the element you want (islice(readCSV, rowID, rowID+1).next()), though a neater solution would just be to store a dictionary mapping rowID to the AOI row when you read it the first time (in the SearchStrings loop):

    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add columns to an existing csv file ,but i can't find
I have to add a few columns to a table and I also need
I have a DataGridView and I need to add several (say 20) columns to
I have a SQL query (MS Access) and I need to add two columns,
Need help pretty soon on this..... I have a Gridview in which the columns
I need to add indexes to my table (columns) and stumbled across this post:
I need to add a couple of columns to a table that is a
I need add Auto complete on apex Tabular Form. there is a column as
I have a table of data that I need to dynamically add a column
I need to add an image/icon to the last column in the row. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.