Select to view content in your preferred language

How to update a feature's attributes via a CSV file with Python

7520
12
Jump to solution
06-19-2015 06:57 AM
AnthonyAtkins1
Deactivated User

I have a CSV file that contain fields that denote, account number, a path to a tiff image and type of account (sewer, water, etc). I'd like to update my Account feature class with the path of the tiff file (for hyperlinking) for matching accounts. At first this sounded pretty simple, just use a search cursor and an update cursor within nested for loops and tada...feature updated!  Well for some reason it's not working and I don't know why. I've tried using nested for loops, I've tried creating the cursors via with statements and i can't get it to work.

here's the portion of the code I'm having issue with:

searchCSV = arcpy.da.SearchCursor(outputcsvlocation2, ["AccountNumber","Path","Type"])
updateAccount = arcpy.da.UpdateCursor(fc, ["AcctNum","B_SCAN", "W_SCAN", "S_SCAN", "ST_SCAN"])

for row in searchCSV:
    for line in updateAccount:
        if row[0] == line[0]:
            print"This is where I'd update the feature!"

To keep things simple, I've been using smaller datasets; my csv file only has two records, with account numbers 101010000 & 101011002 and my feature class only has five features with account numbers: 101010000, 101000100, 101011000, 101011001, 101011002.

Following my logic, my print statement should have printed twice, but it didn't print at all?  I know my issue is some sort of logic, but I can't quite see it. please help!

fyi, I also tried this:

with arcpy.da.SearchCursor(outputcsvlocation2, ["AccountNumber","Path","Type"]) as searchCSV:
    with arcpy.da.UpdateCursor(fc, ["AcctNum","B_SCAN", "W_SCAN", "S_SCAN", "ST_SCAN"]) as updateAccount:
        for row in searchCSV:
            for line in updateAccount:
                if row[0] == line[0]:
                    print"This is where I'd update the feature!"
0 Kudos
12 Replies
JamesCrandall
MVP Alum

No-open policy on zip files.  Even just a simplified look would help. Like,

cvs sample:

Field1 (integer)     Field2 (string)     Field1 (decimal)

1                           station 1              0.19

2                           station 2              3.94

3                           station 4              9.33

...or something to that effect.

0 Kudos
AnthonyAtkins1
Deactivated User

My apologies!

Account attributes (reduced the # of fields to only the needed ones)

     

AcctNumB_SCANW_SCANS_SCANST_SCAN
101010000\Current_Scans\Scans\101010000S.tiff
101000100
101011000
101011001
101011002\Current_Scans\Scans\101011002W.tiff

CSV attributes:

   

AccountNumberPathType
101010000\\gisserver\GIS\CLIENT\m_Scans\Current_Scans\Scans\101010000S.tiffSewer
101011002\\gisserver\GIS\CLIENT\m_Scans\Current_Scans\Scans\101011002W.tiffWater
0 Kudos
JamesCrandall
MVP Alum

This works from the method to Join-then-calculate idea mentioned.  The issue I always run into with using joined data is that:

1. Field names get weird.

2. I have to deal with a new Feature Class and do something with the original one before joined.

fgdb = r'H:\ForumThread.gdb'
csvfile = r'H:\jointab.csv'

arcpy.TableToTable_conversion(csvfile, fgdb, "csvsource")

fc = fgdb + "\\MyFC"
fl_fc = "theFC"

if arcpy.Exists(fl_fc):
 arcpy.Delete_management(fl_fc)

tab = fgdb + "\\csvsource"
tv_tab = "theTAB"
if arcpy.Exists(tv_tab):
 arcpy.Delete_management(tv_tab)

arcpy.MakeFeatureLayer_management(fc, fl_fc)
arcpy.MakeTableView_management(tab, tv_tab)
arcpy.AddJoin_management(fl_fc, "AccountNo", tv_tab, "AccountNumber", "KEEP_ALL")

joinFC = fgdb + "\\MyFC_join"

if arcpy.Exists(joinFC):
 arcpy.Delete_management(joinFC)

arcpy.CopyFeatures_management(fl_fc,  joinFC)
 
fields = ('MyFC_S_SCAN','MyFC_W_SCAN','csvsource_Path','csvsource_Type')
with arcpy.da.UpdateCursor(joinFC, fields) as cursor:
 for row in cursor:
  if (row[3]==' Sewer '):
   row[0]=row[2]
  elif (row[3]==' Water '):
   row[1]=row[2]
  cursor.updateRow(row)
0 Kudos