Select to view content in your preferred language

Deleting double quotation " in field, find/replace errors - script

5391
14
01-12-2011 01:01 PM
TurnerNowak
Deactivated User
I have several shape files (all Parcels.shp) that I run scripts on in an attempt to clean and standardize the address field (SIT_FULL_S).  Whenever a record has a double quotation, I get an error and the script shuts down.

Here is what I am using:

gp.CalculateField_management(fc + "\\Parcels.shp", "SIT_FULL_S", "!SIT_FULL_S!.replace(u'\u0022',u'\u0027')", "PYTHON", "")

example 1 - Script works with this text:          125 South  Main Street
example 2 - Script shuts down with this text:  125 South "Main Street"

When I try example 1, the scripts runs.
When I try example 2, the script quits with Error 000539

Long story short, how can I remove any occurances of " from my field so that I can run my address scrubbing scripts ?


Or perhaps I should ask how to modify my below script to ignore the " and continue running. Here is an example of one of my address scrubbing scripts and the log showing the error:

gp.CalculateField_management(fc + "\\Parcels.shp","SIT_FULL_S", "!SIT_FULL_S!.lstrip('0')", "PYTHON")
arcgisscripting.ExecuteError: ERROR 000539: Error running expression: "9030 W SR 2 HWY "A"".lstrip('0') <type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 1)
Failed to execute (CalculateField).
Tags (2)
0 Kudos
14 Replies
by Anonymous User
Not applicable
Couple of things.  One method I use for this is...
gp.CalculateField_management(fc + "\\Parcels.shp", '"%s" % (!SIT_FULL_S!.replace('"','')'), "PYTHON", "")


Regarding the code and your error.  The 999999 is very generic.

Looking at this code, I don't see how it could possibly work as intended.

Why are you looping through all the objects in ListWorkspaces() and then doing rows = gp.UpdateCursor("//Parcels.shp")?  Aren't you just reopening the same shape file?

Couldn't you just do?

rows = gp.UpdateCursor("//Parcels.shp")
for row in rows:
    row.name = row.GetValue("Name").replace("-", "")
    rows.updateRow(row)
del row
del rows


import arcgisscripting
import logging 
logger = logging.getLogger()

gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

for fc in fcs:
    print fc
    rows = gp.UpdateCursor("//Parcels.shp")
    row = rows.Next()
    while row:    
        name = row.GetValue("Name")        
        dash = string.find(name, "-")     
        if dash != -1:
            fix = name.replace("-", "")
    del row, rows
0 Kudos
TurnerNowak
Deactivated User
Thanks for the suggestions Huey.

When I try your calculate field script I get this:

C:\Documents and Settings\Andrew>"C:\Python24\New Text Document.py"
  File "C:\Python24\New Text Document.py", line 19
    gp.CalculateField_management(fc + "\\Parcels.shp", '"%s" % (!SIT_FULL_S!.rep
lace('"','')'), "PYTHON", "")

                      ^
SyntaxError: invalid syntax



And when I try your update cursor script I get this:


C:\Documents and Settings\Andrew>"C:\Python24\update cursor test.py"
C:\ZP4\TEST2
Traceback (most recent call last):
  File "C:\Python24\update cursor test.py", line 15, in <module>
    for row in rows:
TypeError: 'geoprocessing cursor object' object is not iterable
0 Kudos
LoganPugh
Frequent Contributor
The "for row in rows" syntax was introduced with arcpy in ArcGIS 10. For 9.3 and earlier you'll need to use the following construct:

row = rows.Next()
while row:
   <do stuff>
   row = rows.Next()
del row, rows
0 Kudos
TurnerNowak
Deactivated User
Thanks lpugh01 , I made those changes and modified a few other lines to come up with a script that works.
Here is what I have now :

import arcgisscripting
import logging 
logger = logging.getLogger()

gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

for fc in fcs:

    print fc
    rows = gp.UpdateCursor(fc + "//Parcels.shp")
    row = rows.Next()
    while row:
        Name = row.GetValue('APN').replace('"', '')
        print Name
        row.SetValue('Apn', Name)
        rows.updateRow(row)
        row = rows.Next()
    del row
    del rows


0 Kudos
PaulBrandt
Regular Contributor
From the code suggestions in this thread (credit all who shared) I put together a model tool that you can use straight up with python code blocks to calculate fields when you have double quotes in a field.
0 Kudos