Solved! Go to Solution.
This method seems a little dangerous as I can envision where the string "0a" could occur in your hex string by accident, for example the two byte hex "b0 ac" represented this way would be an invalid match.
Here's a function implementation of your approach, not using hex codes. You could paste this function into the ArcMap python window and use it there. Note I'm using chr(10) and chr(13) for "\n" and "\r" so this function can also be used inside the Calculate Value tool in modelbuilder... as the usual use of "\n" breaks the geoprocessing messaging string representation in the code...
I'm also using arcpy.da.UpdateCursor because it is very fast compared to the 10.0 flavor.... and the "with" construct helps you out by closing the cursor even if the code fails -- avoiding the possibility of a nasty hanging file lock!
import arcpy
def strip_newlines(tbl, field, eolchar=""):
with arcpy.da.UpdateCursor(tbl, [field]) as rows:
for row in rows:
row[0] = row[0].replace(chr(10), eolchar).replace(chr(13), eolchar)
rows.updateRow(row)
Try dumping the table to a text file, if you think there is stuff going on in there you cannot see.
There is an 're' module in python that handles regular expressions. That is the tool set you want for weeding out pesky newlines ('\n').
It may be easier to weed them in the table or in the text dump (which could then be re-imported to a table).
#ESRI Codeblock codeblock="""def trimNewline(val): import re newVal = re.sub('(?m)[\r\n]',"",val) return newVal""" #Expression parameter expression = "trimNewline(str(!FIELD!)) # CalculateField_management(in_table,field,expression,{expression_type},{code_block}) arcpy.CalculateField_management(mytable,"FIELDNAME",expression,"PYTHON",codeblock)
>>> x '\r\nhere is my real text\n' >>> x.strip() 'here is my real text'
arcpy.CalculateField_management(mytable,"FIELDNAME","!FIELDNAME!.strip()","PYTHON")
arcpy.CalculateField_management(mytable,"FIELDNAME","Trim([FIELDNAME]")
rows = arcpy.UpdateCursor(fc) for row in rows: if len(row.NAME) >= 255: hexString = str(row.NAME).encode("hex") if "0a" in hexString: # "0a" is hex equivalent of '\n' hexString = hexString.replace("0a","") row.NAME = hexString.decode("hex") rows.updateRow(row)
This is the solution that worked for me, though I removed the len(row.NAME) >= 255 requirement.
Here's the working example:
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:
import arcpy import string
I just wanted to make this idiotproof because I struggled for a bit (I copied and pasted your example expecting it to work). I'm still somewhat new to (and still learning) Python so I'm sure I'm not the only one who will forget to change the appropriate variables to their ArcGIS data.
This method seems a little dangerous as I can envision where the string "0a" could occur in your hex string by accident, for example the two byte hex "b0 ac" represented this way would be an invalid match.
Here's a function implementation of your approach, not using hex codes. You could paste this function into the ArcMap python window and use it there. Note I'm using chr(10) and chr(13) for "\n" and "\r" so this function can also be used inside the Calculate Value tool in modelbuilder... as the usual use of "\n" breaks the geoprocessing messaging string representation in the code...
I'm also using arcpy.da.UpdateCursor because it is very fast compared to the 10.0 flavor.... and the "with" construct helps you out by closing the cursor even if the code fails -- avoiding the possibility of a nasty hanging file lock!
import arcpy
def strip_newlines(tbl, field, eolchar=""):
with arcpy.da.UpdateCursor(tbl, [field]) as rows:
for row in rows:
row[0] = row[0].replace(chr(10), eolchar).replace(chr(13), eolchar)
rows.updateRow(row)
This worked for me in 10.2. For some reason the old methods that worked in 10.0 no longer worked in 10.2 so I'm glad I found your solution Curtis. Thanks for sharing!