Select to view content in your preferred language

Update attribute table using Arcpy

3866
5
11-15-2017 09:18 AM
Henryobaseki
Regular Contributor

Hi All,

 This  python code seems to work for updating  two tables saw it online, with my low arcpy code skills

This seem to update the two tables

import arcpy
... from arcpy import env
... env.workspace = "C:\Users\L0505857\Data"
... print 'Processing...'
... fc = "Updated_subset.dbf"
... cursor = arcpy.da.SearchCursor(fc, ["RESID"])
... for row in cursor:
...     SQL_stat= "RESID = "+ str(row[0])
...     fc2 = "airports_old.dbf"
...     cursor2 = arcpy.da.SearchCursor(fc, ["FIELD_NAME"], SQL_stat)
...     for row2 in cursor2:
...         UpdatedValue = row2[0]
...         cursor3 = arcpy.da.UpdateCursor(fc2, ["FIELD_NAME"],SQL_stat)
...         for row3 in cursor3:
...             row3[0] = UpdatedValue
...             cursor3.updateRow(row3)
... del row
... del cursor
... del row2
... del cursor2
... del row3
... del cursor3
...
... print "Done"

thisSQL_stat line ---SQL_stat= "RESID = "+ str(row[0])

converts the  unique ID field  "RESID  Field"  to string.

How do I remove the str for a another table  in which the RESID field is already a string and not an interger field?

 SQL_stat= "RESID = "+ (row[0])

tried the line above without the str, I m getting errors.

Thanks for your help

Tags (1)
5 Replies
JoshuaBixby
MVP Esteemed Contributor
0 Kudos
DarrenWiens2
MVP Honored Contributor

Include the full error message. There shouldn't be a problem trying to cast an existing string to a string (e.g. str("my_string")).

Henryobaseki
Regular Contributor

Hi Darren,

>>> import arcpy

... from arcpy import env

... env.workspace = "C:\Data"

... print 'Processing...'

... fc = "Updated_subset.dbf"

... cursor = arcpy.da.SearchCursor(fc, ["RESID"])

... for row in cursor:

... SQL_stat= "RESID = "+ str(row[0])

... fc2 = "airports_old.dbf"

... cursor2 = arcpy.da.SearchCursor(fc, ["Regional_s"], SQL_stat)

... for row2 in cursor2:

... UpdatedValue = row2[0]

... cursor3 = arcpy.da.UpdateCursor(fc2, ["Regional_s"],SQL_stat)

... for row3 in cursor3:

... row3[0] = UpdatedValue

... cursor3.updateRow(row3)

... del row

... del cursor

... del row2

... del cursor2

... del row3

... del cursor3

...

... print "Done"

...

Processing...

Runtime error

Traceback (most recent call last):

File "<string>", line 11, in <module>

RuntimeError: A column was specified that does not exist.

see the runtime error

Thanks

0 Kudos
RandyBurton
MVP Alum

Since you are creating an SQL where clause, you may need the string in quotes for comparison.  It may be helpful to use something like:

>>> row =  ( 123, "hello")
>>> sql = "RESID = {}".format(row[0]) # for numbers
>>> sql
'RESID = 123'
>>> sql = "RESID = '{}'".format(row[1]) # for strings
>>> sql
"RESID = 'hello'"‍‍‍‍‍‍‍
RebeccaStrauch__GISP
MVP Emeritus

It would help if you edit you post and post the code using https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...‌  or /blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=a483b6b2-88cf-4672-bb4a-703edf9...‌ 

What I see right off in what you post, assuming the spacing is how you are trying to use it, is all the lines that are indented (line 2 down) shouldn't be, until you get to the "for row in cursor:"     then some of the lines below that might need to be.  I did not look at the entire code, but fixing the indentation is probably the first step (again, assuming the spacing shown is what you are trying to use).