Sunday, 2 June 2019

arcgis desktop - Removing return/newline (n) character from Field using Python and Field Calculator?


I have a geodatabase table with a field that I'm trying to remove return characters (new line). I found this post (How can I remove (chomp) a newline in Python?) of how to do it, however it is not working within field calculator. Here are the code snippets that I tried: Note return character is not at the end of the string.


!myField!.rstrip()


OR


!myField!.rstrip('\n')

OR


!myField!.rstrip('\r\n')

OR


!myField!.replace('\n', '')

000539 error given for this option:




Description The calculation used by the Calculate Field or Calculate Value tool is invalid. This error message provided will list the specific Python error.


Solution This error code covers a number of Python errors:


Example error 1: exceptions.TypeError: cannot concatenate 'str' and 'int' objects. The above is a Python-specific error. The calculation is attempting to add or concatenate a string and a number.


Example error 2: Invalid field shape@distance The above is an error using the geometry object. The distance method is not a valid method of the geometry object.


For specific Python issues, consult the external Python help for more information, or consult the Calculate Field or Calculate Value help for more information on these tools.



OR


import os
def removeReturn(myField):

s = myField.rstrip(os.linesep)
return s

Any ideas of how to remove return characters using field calculator?



Answer



The work around for this since my feature class table originated in excel was to use the following excel command:



=CLEAN



method to remove all return or new line characters. You can then join or import the table into your GIS database.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...