
Originally Posted by
janvanlinge
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:
Code:
hyperlink = "c:\somehyperlink\name_of_file"
arcpy.CalculateField_management(TableToEdit, "HYPERLINK_FIELD", r"r'" + hyperlink + r"'", "PYTHON")
The above code embeds a newline (\n) in the string:
Code:
>>> print "c:\somehyperlink\name_of_file"
c:\somehyperlink
ame_of_file
I think this may work better:
Code:
>>> print 'r"{0}"'.format(r"c:\somehyperlink\name_of_file")
r"c:\somehyperlink\name_of_file"
Code:
hyperlink = r"c:\somehyperlink\name_of_file"
arcpy.CalculateField_management(TableToEdit, "HYPERLINK_FIELD", '{0}"'.format(hyperlink), "PYTHON")
I thought I'd add one more thing to this post: how to specify newlines in the Calculate Field tool in ModelBuilder. The interactive tool dialog parser converts "\n" to real newlines in the code box, which doesn't work, so the workaround is to to use chr(10). I've used this as a quick and dirty way to have model builder print a message:
Expression: msg()
Code:
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
Bookmarks