Error 539 while running CalculateField to copy strings

1900
4
07-31-2012 05:08 AM
BärbelHorat
New Contributor
Hi,

I'm trying to copy all information from one string field to a new one in the same feature class. I am using this code:

#Feature class:
Tab = r'\\Tako\gis\03_basisdaten\pronatura\ch\gisdata\pronatura_1.gdb\Schutzgebiete\parz_pn'
#Field:
orgField = "Bemerkungen"
#Length of Field:
Length = 1000


#Adding the new Field:
provField = orgField + "_prov"
AddField_management(Tab,provField,"TEXT", "", "", Length)

#Copy the information from the original field to the new one:
Expression = 'str(!%s!)' %(orgField)
CalculateField_management(Tab, provField, Expression, 'PYTHON')


The tool stops at feature 52 of 2500 and gives this Error:

Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: str("1x jährlich im Herbst zu mühen. Er verwendet Flurname "Hiwes", vormals Parz. 200310  ") <type 'exceptions.SyntaxError'>: invalid syntax (<string>, line 1) Failed to execute (CalculateField).


I assume there is some problem with the " in the string to copy. How can I tell Python to treat the string as one and ignore all special characters?
Tags (2)
0 Kudos
4 Replies
KatharinaPalffy-Gelfand
New Contributor
Hi Bärbel,

just use the field calculator or try the following reworked script:

#Feature class:
Tab = r'\\Tako\gis\03_basisdaten\pronatura\ch\gisdata\pronatura_1.gdb\Schutzgebiete\parz_pn'
orgField = "Bemerkungen"
provField = "Bemerkungen_prov"

#Adding the new Field:
arcpy.AddField_management(Tab,provField,"TEXT", "", "",254)

#Copy the information from the original field to the new one:
arcpy.CalculateField_management(Tab, provField, orgField, 'PYTHON')

That should work, if both fields are text fields. There is no need to convert the field to str first.
Thanks,
Kat
0 Kudos
BärbelHorat
New Contributor
Thanks for your input, Kat. Unfortunately it still does not work. My script does fine with test data consisting of short strings without special caracters.

Your reworked script is not working because of the missing '!' for the field name. If i add the '!', it works with my test data but not with the one i want to work with. Same error.

#Copy the information from the original field to the new one:
arcpy.CalculateField_management(Tab, provField, !Bemerkungen!, 'PYTHON')


I tried it in ArcMap with FieldCalculator. This gives me a new Error. (With test or real data)

000732: Dataset does not exist or is not supported
Witch is quite strange, as I am able to add the data to ArcMap and start the Field Calculator.

Someone else with an Idea?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
What's different with feature 52 than the others?  Seems like this feature is causing the rest of the script to fail.  Could you send a screen shot of your attribute table (include feature 52 in the screen shot)?
0 Kudos
BärbelHorat
New Contributor
I now worked some hours on the problem. The Problem are Features (like Feature 52) with ". It seams as CalculateField uses " as string delimiter. It now it workes with those features, if I first substitute all " with ' (using find & replace in ArcMap). Is there a posibility to deal with the " using a script?
The script now causes errors at features with an line break within the textfield. Any ideas how to deal with them in the script? The new Error:


Runtime error <class 'arcgisscripting.ExecuteError'>: 
ERROR 000539: Error running expression: "servitude de 'passage à pied' non aménagé sur parcelle 4259
 servitude de 'passage à pied' non aménagé" 
<type 'exceptions.SyntaxError'>: EOL while scanning string literal (<string>, line 1)
Failed to execute (CalculateField). 
0 Kudos