Trying to write a python script to access data from attribute tables

3933
23
Jump to solution
07-22-2014 09:28 AM
ThomasCaruso
New Contributor III

Hi folks. I'm new to both GIS and programming and am attempting to combine the two. I need a python script that will read down a particular column of the attribute table of a particular layer, then check it against a column in the attribute table of another layer. Both of these columns will contain strings. If the two match, then the script should input "YES" into another field in the first layer.

I honestly have no idea how to even start this. I've read through esri's help section and have a very basic grasp of python but I'm unsure of how to get the script to actually access the attribute tables. Any help would be very greatly appreciated.

Thanks in advance! If there's a better place to post this, please let me know.

Tags (2)
23 Replies
FlorianHoedt2
New Contributor III

please give us some example names for a) working and b) not working so we could figure out what is wrong with your approach.

0 Kudos
ThomasCaruso
New Contributor III


This is what's really been confusing me. There doesn't seem to be any rhyme or reason. For example,

Mike Crane was split perfectly fine, Bob Wyant, Bernie Vossler, Sherwood Lidermore all fine. But Tod Shaw, John Markel, Chris Mendell did not split. I checked to see if the ones that split had a space after their last name, but they didn't.

Furthermore, some people who had three names or a middle initial did or didn't split, like "Paul And Anna Marie" gave me Paul And, but Rod And Cindy just gave me Rod.

The error messages I got were:

Executing: calculate field wellparcelloc OwnerLast !Owner!.split(" ")[1] PYTHON_9.3#

ERROR 000539: Error running expression: u"MINNEKINE".split(" ")[1]

traceback:

file "<expression>", line 1, in <module>

IndexError: list index out of range

0 Kudos
FlorianHoedt2
New Contributor III

ERROR 000539: Error running expression: u"MINNEKINE".split(" ")[1]

traceback:

file "<expression>", line 1, in <module>

IndexError: list index out of range

'MINNEKINE' is the string which should be split by ' ' (space), but it has no space. Therefore the list which is returned has no second (list[1]) entry -> index out of range.

Seems like you have OWNER values which have no space as seperator, why? Do you perform any other method on !OWNER! before putting it into split()?

0 Kudos
ThomasCaruso
New Contributor III

No, this split is the only method being performed. Would running into a single string without a space stop the whole process?

0 Kudos
IanMurray
Frequent Contributor

Thomas,

I would use [-1] for the Owner Last Name field, that way you get the last record, regardless of if there are two names on the record.  Unfortunately it looks like you will need to manually fix some of your data, since like the example you showed, there are records where someone did not put in a space.  These records seem pretty poor, as some people used last names, and some didn't so you will probably have to spot check alot of this unfortunately.

ThomasCaruso
New Contributor III

Ian,

Using [-1] totally fixed the problem; I didn't realize that was an option. Thank you! I will have to do some spot checking, but that's fine. this has already saved me time. I'd like to sincerely thank both you and Florian- you've helped TREMENDOUSLY.

0 Kudos
IanMurray
Frequent Contributor

also as far as the capitilization being off, when you run the script when you have the row[0] == row[1], change it to row[0].upper() == row[1].upper().  This will match those where there is mixed capitalization since it makes all the characters in the string uppercase, so you can't mix and match upper and lowers.

Also I might do something where I check the length of !Owner!.split(" "), and if it is 1, have a print statement or write to a text file, that way you know which ones have only a single name.

FlorianHoedt2
New Contributor III

just to answer this:

"Would running into a single string without a space stop the whole process?"

Yes, it would. You have to use a try: / except: call to not get stopped by a missing method call. You could look it up in the python API. The try block will get executed and if it can not execute properly the except block will get called instead - which will relate to:

1. you can print an error log with additional info in the except block

2. the script won`t stop cause the error get catched by the exception block

JeffreyVu
New Contributor

It would be very easy if you are using FME (www.safe.com), no programming needed.

You can use the following transformers:

FeatureMerger to identify matched records, AttributeCreator for MERGED and NOT_MERGED with Yes and No, respectively.

SpatilalFilter to identify the spatial relationships.

0 Kudos
ThomasCaruso
New Contributor III

so....is this even remotely correct? Am I even close to being on the right track?

import arcpy

wells = "path here"
parcels = "path here"
owner = ["Owner"]
powner = ["Owner1"]
cursor = arcpy.da.UpdateCursor(wells, ["Public"])

wellowner = arcpy.da.SearchCursor(wells, owner)

parcelowner = arcpy.da.SearchCursor(parcels, powner)

if ( wellowner == parcelowner ) :
     for row in cursor:
           row = YES

else row = cursor.next()