Removing return/newline (\n) character from Field using Python and Field Calculator?
I think this is just a bug/limitation of the Python parser with the field calculator/Calculate Field tool. If a newline is encountered within the text field, a SyntaxError: EOL while scanning string literal
occurs no matter what you try.
I can reproduce the issue at 10.1 SP1 by importing the following CSV file into a file geodatabase, adding a field and simply attempting to copy the Text
field to the new field using the Field Calculator with the Python parser and and the expression !Text!
.
ID,Text 1,"this is a multiline example"
Try switching to the VB parser, or using an UpdateCursor
and avoiding the Field Calculator entirely.
This issue is also discussed on the ESRI forums, with the same conclusions:
- End-of-line (EOL) Problem
The only relevant NIM I could find was this:
- NIM085499 - CalculateField calculations using non-ASCII characters fail on a Linux engine with: "ERROR 000539: SyntaxError: EOL while scanning string literal (, line 1)".
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.
There's two possible solutions I've found to be reliable. For some reason CartoPac allows people to put a Return in the REMARKS field where I work. To get rid of those, the solution that I've found to work best is using the Field Calculator.
Change your Parser to Python. Put the following in Pre-Logic Script Code:
def carriageReturnRemoval(remark):
remark = remark.splitlines()
separator = " -- "
return separator.join(remark)
Put the following in the next text box (this one is working on a field called REMARKS):
carriageReturnRemoval( !REMARKS! )
When your carriage returns get removed, it will add a space, --, and a space between each line. You can modify " -- " to a different character or set of characters if you'd like. But I've found this to work best for me based on the way my construction crews enter data. It is easier to do Find/Replace operations if there are recognizable character patterns breaking up each carriage return, which they use in the field to denote a new attribute (even though it is often an attribute they could've just put into the proper field and saved me the headache in the first place).
If you'd prefer to use the Python Console in ArcGIS, you can modify the above to work. However I've also tried this with some success in the Python Console:
rows = arcpy.UpdateCursor("Assets\Welds")
for row in rows:
hexString = str(row.REMARKS).encode("hex")
if "0a" in hexString:
hexString = hexString.replace("0a","")
row.REMARKS = hexString.decode("hex")
rows.updateRow(row)
Replace "Assets\Welds" with the appropriate fc name and replace row.REMARKS with row.(insert field name here)
You may or may not need to run the import lines below before attempting the code example above:
import arcpy
import string