GetParameterAsText using 'r' like raw strings?

2482
9
Jump to solution
04-05-2012 11:19 AM
Zeke
by
Regular Contributor III
I've looked everywhere I can think of for a solution, or at least similar problem, to this but can't find one.

The script gets a user entered value via GetParameterAsText(0).

The value normally contains '-' characters, a typical one taking the form Z-FY-12-34, for example. When I try to set a text field to this value, CalculateField apparently treats the value as a number, subtracting the numbers and discarding the letters; the value then gets set to something like -46. I'm not sure it actually subtracts, but it always returns a - followed by a number. Whenever I check the value in the script, it looks correct.

I'm guessing that maybe this is related to using r to accept a raw string, but I don't know how to do that with GetParameter..., and maybe that's not the actual problem anyway. Any ideas on what the cause is and how to fix it? Thanks.

case_number = arcpy.GetParameterAsText(0)   # user enters Z-FY-12-34 . . .  fc = "Cases"     fld = "Case_"      arcpy.AddMessage("Adding Case number " + case_number + " added.")   # case_number = Z-FY-12-34      try:         arcpy.CalculateField_management(fc, fld, case_number)                  # field value set at -124 or some other number         arcpy.AddMessage("Case number " + case_number + " added.")   # case_number = Z-FY-12-34     except Exception as e:         arcpy.AddError("Error setting Case_ field value to case number. \n" + e.message)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
No, raw strings only apply to string literals not string variables.  You just need to quote your expression as you would entering it manually in the field calculator:
arcpy.CalculateField_management(fc, fld, '"'+case_number+'"') #Or arcpy.CalculateField_management(fc, fld, '"%s"' % case_number)

View solution in original post

0 Kudos
9 Replies
Luke_Pinner
MVP Regular Contributor
No, raw strings only apply to string literals not string variables.  You just need to quote your expression as you would entering it manually in the field calculator:
arcpy.CalculateField_management(fc, fld, '"'+case_number+'"') #Or arcpy.CalculateField_management(fc, fld, '"%s"' % case_number)
0 Kudos
Zeke
by
Regular Contributor III
Thanks. I didn't think that would be necessary using the variable from a parameter, but very helpful to know.
0 Kudos
KariBuckvold
Occasional Contributor

Did you ever figure this out?  I am trying to use arcpy.CalculateField_management to add the filepath and filename to a field and if the folder begins with a number, it distorts the name. For example a portion of the file path might look like:

...\Date\Client\180705\1_Event\Point.shp

will look like:

...\Date\Client   180705  @_Event\Point.shp

 

I will want do calculations in other fields based on these values using the .split("\") function, so it's important that I get the complete, correct filepath. 

0 Kudos
Luke_Pinner
MVP Regular Contributor

Did you ever figure this out?

The answer is to quote the strings in the expression. 

If you're having trouble, put some more detail (i.e some code would be good) in about your input values and how you construct the expression.

Also, don't parse paths with  .split("\")... Use os.path.split(your_file_path) which will handle all the slash permutations for you.

KariBuckvold
Occasional Contributor

Here is what the code looks like (not sure how to add to this as a code sample, just as text.)  I've also tried adding an "r" in various forms in the CalculateField line, to no avail. When using the print function for the fc, the filepath/name appears as it should.

The parsing with the .split("\") was for a later field calculator expression, but mentioned because to indicate that it is important that the file path be exact.  

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
       fcs.append(os.path.join(dirpath, filename))
 
for fc in fcs:
   print fc
   arcpy.CalculateField_management(fc, 'Source', '"' + fc + '"' , "PYTHON")
0 Kudos
Luke_Pinner
MVP Regular Contributor

Try

arcpy.CalculateField_management(fc, 'Source', 'r"' + fc + '"' , "PYTHON")
0 Kudos
KariBuckvold
Occasional Contributor

that. was. it.

Thank you, I was pounding my head against the wall on that one!

0 Kudos
Luke_Pinner
MVP Regular Contributor

What you (and lots of others) were probably struggling with is you need to construct a valid python expression inside a python string, not as an actual python expression

For example, say you wanted to calculate a string field to be something like r"A:\B\D.d", if you passed that to arcpy.CalculateField_management, the tool would only see A:\B\D.d which is not valid python (it's not quoted) and the backslashes wouldn't be escaped.  You would construct your expression as r'r"A:\B\D.d"'. There's a few ways to make this easier to get your head around with string formatting. 

In simple terms, think about what you need to have valid python syntax, then wrap all that in quotes to make a string.

0 Kudos
DanPatterson_Retired
MVP Emeritus

string split with backslashes need to be escaped ie "\\"

the differences are as in the example

win_path = 'C:\\GIS\\A_Tools_scripts\\Polygon_lineTools\\Scripts\\arcpytools_plt.py'

win_path.split("\\")

['C:',
 'GIS',
 'A_Tools_scripts',
 'Polygon_lineTools',
 'Scripts',
 'arcpytools_plt.py']

os.path.split(win_path)

('C:\\GIS\\A_Tools_scripts\\Polygon_lineTools\\Scripts', 'arcpytools_plt.py')
0 Kudos