2

I'm trying to update a feature class with new data from a csv file. My workflow is as follows:

  1. Make a csv reader from csv file
  2. Identify my data columns using their index position
  3. Use an update cursor to update the values in the shapefile with data from the csv

Here is my code so far:

import csv
import arcpy
csvfile = "C:\\Data\\data.csv"
shpfile = "C:\\Data\\TEST.gdb\\azcounties"
file = open(csvfile,"r")
csvReader = csv.reader(file)
header = csvReader.next()
nameIndex = header.index("NAME")
dateIndex = header.index("DATE")
valueIndex = header.index("VALUE")
dateField = "DATE"
nameField = "NAME_2"
valueField = "VALUE"
with arcpy.da.UpdateCursor(shpfile,(nameField,dateField,valueField)) as cursor:
 for row in cursor:
     for rw in csvReader:
         row[0] = rw[nameIndex]
         row[1] = rw[dateIndex]
         row[2] = rw[valueIndex]
         cursor.updateRow(row)

It seems to almost work. I don't get any errors, but only the first row in the shapefile gets updated.

closed as off-topic by PolyGeo Aug 12 '15 at 23:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • What are you trying to do? The inner loop (rw in csvReader) is just going to replace the value of every row in the outer loop with the last record of the csvReader. Plus, you're calling updateRow on every loop of the csvReader which is a slow down and doesn't fully make sense. – Branco Aug 13 '15 at 16:42
  • I'm trying to loop through a csv file with 3 columns and 15 rows and update the corresponding fields in a shapefile. Each iteration I'm trying to pull the current row from the csv file and update the current row in the shapefile. – a_h Aug 13 '15 at 16:49
  • So the feature class record 1 needs information from the 1st row in the CSV? Are there unique identifiers so you could just do a join and save the data that way? – Branco Aug 13 '15 at 16:53
  • Yes, feature class row 1 gets data from csv row 1. Is there a way to do this? – a_h Aug 13 '15 at 17:04
  • Doing a join could work but then I have to strip off old fields, and may not always have common field. – a_h Aug 13 '15 at 17:10

Browse other questions tagged or ask your own question.