GIS tutorial for Python Scripting written by David Allen

3567
12
09-17-2014 06:05 PM
Jeremiah_OlatundeGbolagun
New Contributor

Please let me know if the below code is right or not. if it is wrong can you please correct it for me.

I changed the below code from the above titled book during learning Tutorial 1, which was titled "Use Python in the Label Expression dialog box". I am using ArcGIS 10.1 but the tutorial used ArcGIS 10.2.

This is the original code from the text book.

def FindLabel ( [OwnerName] 😞

  rawName = [OwnerName]

  commaNum = rawName.find(",")

  formatName = rawName[commaNum+2:] + " " + rawName[:commaNum]

  return formatName

When I run this above code I have this error "SyntaxError: invalid syntax (<string> , line 4"

Can you please let me know the right code to use.

I modify it to below code but still experience another error.

def FindLabel ( [OwnerName] 😞

  rawName = [OwnerName]

  commaNum = rawName.find(",")

  formatName = rawName(commaNum[2:]) + " " + rawName(commaNum[0:]

  return formatName

Regards,

Jeremiah.

0 Kudos
12 Replies
curtvprice
MVP Esteemed Contributor

I bet this doesn't work because within the label engine brackets are interpreted as field names. The examples in the help do not use brackets in the python examples and I think that is why!  My guess is Mr. Allen tested the code at the Python prompt (not inside the label engine) and called it good, though I could be wrong.

Here's a method that avoids the brackets and takes advantage of the split, join, and reverse list methods. Neat, huh?

def FindLabel ( [OwnerName] ):

  rawName = [OwnerName] # "Last,First"

  names = rawName.split(",") # ["Last", "First"]

  formatName = " ".join(names.reverse()) # "First Last"

  return formatName

0 Kudos
MichaelAugust
Occasional Contributor III

def FindLabel ( [OwnerName] 😞

  rawName = [OwnerName]

  commaNum = rawName.find(",")

  formatName = rawName[:commaNum+2:] + " " + rawName[:commaNum]

  return formatName

do you need to add a colon before the first commaNum above?

0 Kudos
ChristopherLopez
New Contributor II

Your first set of code worked for me, I just copied and pasted what you had. Just be sure you have advanced checked and your parser is set to Python.

0 Kudos
Jeremiah_OlatundeGbolagun
New Contributor

No I do not think so. This code was copied from the GIS tutorial for Python

Scripting tutorial 1 example. Please note that the code is to be run ArcGIS

10.2. But I ran the code on ArcGIS 10.1 and it gave me error that I sent to

the GeoNet forum site.

0 Kudos
curtvprice
MVP Esteemed Contributor

Pretty strange, sir, your code works for me (10.1 SP 1).

I agree with the advice above to click the advanced checkbox and set the parser to Python before pasting the code.

Capture.PNG

0 Kudos
DavidAllen
Occasional Contributor

Hey Jeremiah, Glad to know that you're working through the book and I'm sorry that you got hung up on this one.

There may be some differences in how the concatenations are handled between 10.1 and 10.2. They did a rewrite on that part of ArcPy to handle some issues with commas, quotes, and such. You'll find a part in the book later where I address that.

Try making a variable for the comma position you want in the first slicing action and see if that helps:

def FindLabel ( [OwnerName] 😞

  rawName = [OwnerName]

  commaNum = rawName.find(",")

  commaNumTwo = commaNum + 2

  formatName = rawName[commaNumTwo:] + " " + rawName[:commaNum]

  return formatName

Not all the Python code errors you get are specifically from the Python error generator - some come from ArcPy and therefore are subject to change. So I try to take any statement where I've some something complex and break it into simple terms and try again. When I've isolated the error, then I can go back and make my code more concise.

Let me know how that goes.

David Allen

Jeremiah_OlatundeGbolagun
New Contributor

Hi David,

Thank you for responding to the Python error I posted on the Geonet forum.

Please note that the code written in your book worked well when I rerun it.

But I later discovered the cause of the error which was due to

inappropriate use of the code indentation. For example, if you do not use

the appropriate number of spaces as you have written in the book it will

result into error. if you checked the error that I posted you will

understand what I meant.

Regards,

Jeremiah.

0 Kudos
DavidAllen
Occasional Contributor

Should have gone with the odds and said indentions …

Ya know, the only thing harder than debugging your code is debugging someone else’s code!!

Let me know if you have any other issues. The exercises are challenging and the steps aren’t shown so if you get stuck on one of those I can send the key.

David

0 Kudos
curtvprice
MVP Esteemed Contributor

Interesting, you indented it correctly in your post! FYI here is how to post code here:

Posting Code blocks in the new GeoNet