1

Would anyone be able to help me modify these scripts to ignore the error and continue running ? I just need to figure out how to make the script skip over these errors and finish the rest of the lines.

Here is the full Python script:

# Import system modules
import sys, string, os, arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

# Set the workspace. List all of the folders within
gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

for fc in fcs:
print fc
gp.CalculateField_management(fc + "\\Parcels.shp", "SIT_FULL_S", "myfunction(!SIT_HSE_NU!,!SIT_FULL_S!)", "PYTHON", "def myfunction(fld1,fld2):\n if (fld1=='0'or fld1=='00'or fld1<'00000000000'):\n   return ''\n else:\n   return fld2")

And here is the error I encounter: Traceback (most recent call last):

  File "C:\Documents and Settings\Andrew\Desktop\HOUSENUMERZERO.py", line 18, in
 <module>

ERROR 000539: Error running expression: myfunction

(" ","69 FLOOD ST
") <type 'exceptions.SyntaxError'>: EOL while scanning single-quoted string (<st
ring>, line 1)

Failed to execute (CalculateField).
dr.parcel
  • 33
  • 1
  • 6
  • @dr.parcel: Kind of. It's still not quite right is it? – S.Lott May 10 '11 at 17:53
  • Whatever script is running those commands needs to properly quote or escape the string, or strip newlines out. Without knowing what's in the script, there's not a lot anyone can do. – Thomas K May 10 '11 at 18:14
  • @dr.parcel: "I've been trying to escape characters for months"?? Then perhaps you should ask *that* question first. Also, while you're at it, please review the code formatting instructions and continue trying to get this question to be readable. – S.Lott May 10 '11 at 20:08
  • I've been searching the site for code formatting instructions but can't find any, can anyone help ? – dr.parcel May 10 '11 at 20:58
  • @dr.parcel: (1) To get formatting instructions, click on the "white ? in red box" button that appears when you are editing your question. (2) The problem appears to be newlines etc embedded in the data. Do you want to remove them, or do you want to escape them so that the script-runner won't throw those error messages? (3) You don't make it plain how these scripts are created. If YOU have written a script-creating script, show the relevant code. – John Machin May 10 '11 at 22:20
  • Thanks John, I formatted and added the script. – dr.parcel May 11 '11 at 17:38

1 Answers1

1

First option: wrap the gp.CalculateField_management(...) in a try/except, like so:

try:
    gp.CalculateField_management(...)
except SyntaxError:
    pass

This should allow your script to keep going, but I'm not sure what the state of gp will be.

A better option would be to preprocess each file, and deal with the fields that have the embedd new-lines in them; something like:

for fc in fcs:
    fix_bad_fields(fp)
    gp.Calculatate...

and fix_bad_fields looks something like (you'll have to research this as I am unfamiliar with .shp files -- I'll pretend it allows writing back to the same file, but if not you'll have to do some copying and renaming as well):

def fix_bad_fields(filename):
    data_file = open_shp_file(filename)
    for row in data_file:
        row[0] = row[0].replace('\n', '')
        row[1] = row[1].replace('\n', '')
        row1.put_changes_on_disk() # force changes to disk (may not be necessary)
    data_file.close()

Lots of guesswork in those particulars, but hopefully that gives you an idea and enough to go on.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237