Solved! Go to Solution.
Or is it just easier to use VB instead of Python for field calculations?
3) Nick's hexidecimal conversion seems to work for \n but, again, how can we apply it to other special characters? Or is it just easier to use VB instead of Python for field calculations?
I ran into the same problem when using python to add hyperlinks. As hyperlinks contain "\" characters it sometimes happened that a "\n" was in the hyperlink. I solved it by passing in the hyperlink as a raw string instead of a normal string:hyperlink = "c:\somehyperlink\name_of_file" arcpy.CalculateField_management(TableToEdit, "HYPERLINK_FIELD", r"r'" + hyperlink + r"'", "PYTHON")
>>> print "c:\somehyperlink\name_of_file" c:\somehyperlink ame_of_file
>>> print 'r"{0}"'.format(r"c:\somehyperlink\name_of_file") r"c:\somehyperlink\name_of_file"
hyperlink = r"c:\somehyperlink\name_of_file" arcpy.CalculateField_management(TableToEdit, "HYPERLINK_FIELD", '{0}"'.format(hyperlink), "PYTHON")
def msg(): # text = "\n\nThis is\na message to you.\n" # does not work text = "{0}{0}This is{0}a message to you.{0}".format(chr(10)) return text
rows = arcpy.UpdateCursor(fc) for row in rows: if '\r\n' in row.TextString: row.setValue('TextString', row.TextString.replace('\r\n', ' ')) rows.updateRow(row) del row del rows
However, if I go from the console and set up something like:rows = arcpy.UpdateCursor(fc) for row in rows: if '\r\n' in row.TextString: row.setValue('TextString', row.TextString.replace('\r\n', ' ')) rows.updateRow(row) del row, rows
It works exactly as one would expect. But I would love to know more about why this doesn't seem to work from the Field Calculator window.
rows = arcpy.UpdateCursor(fc) for row in rows: newline = chr(13) + chr(10) if newline in row.TextString: row.setValue('TextString', row.TextString.replace(newline, ' ')) rows.updateRow(row) del row, rows
The problem is that you cannot use Python escape codes like "\r" in the Field Calculator code block or the Calculate Value code block. I'm assuming this has something to do with the parsing of python arguments into string representation in the arcpy/gp messaging framework.
If you need to access escape characters, use the chr() function instead.
This will probably work fine:rows = arcpy.UpdateCursor(fc) for row in rows: newline = chr(13) + chr(10) if newline in row.TextString: row.setValue('TextString', row.TextString.replace(newline, ' ')) rows.updateRow(row) del row del rows
Could you paste the actual working code you used to get the example working properly?
import os import arcpy tbl = arcpy.CreateScratchName("","","table","in_memory") arcpy.CreateTable_management("in_memory",os.path.basename(tbl)) arcpy.AddField_management(tbl,"TESTFIELD","TEXT") Rows = arcpy.InsertCursor(tbl) Row = Rows.newRow() Rows.insertRow(Row) del Row, Rows arcpy.CalculateField_management(tbl,"TESTFIELD","chr(10) + chr(13)","PYTHON_9.3") print arcpy.GetMessages() Rows = arcpy.SearchCursor(tbl) Row = Rows.next() print "Field value: ",repr(Row.TESTFIELD) del Row, Rows
Executing: CalculateField in_memory\xx0 TESTFIELD "chr(10) + chr(13)" PYTHON_9.3 # Start Time: Mon Aug 05 10:49:33 2013 Succeeded at Mon Aug 05 10:49:33 2013 (Elapsed Time: 0.00 seconds) Field value: u'\n\r'