I have a script and I want to let user type in a value (text). My problem is the user has to put " " for the value they want for any of the newly added fields. ex: "virginia" works virginia does not work. How can I get the toolbox to work without the user typing " " and just type the value. I have looked at the properties parameters of the script and have the data type set to String or Any Value but nothing works. I could find anything online to fix this.
inMosaic = arcpy.GetParameterAsText(0)
yearfield = arcpy.GetParameterAsText(1)
installfield = arcpy.GetParameterAsText(2)
sitefield = arcpy.GetParameterAsText(3)
groupnamefield = arcpy.GetParameterAsText(4)
productnamefield = arcpy.GetParameterAsText(5)
fieldNames = ["YEAR", "INST_ABBR", "SITE_ABBR"]
aliasNames = ["Year", "Installation Abbreviation", "Site Abbreviation"]
fieldsExisting = {f.name: f.type for f in arcpy.ListFields(inMosaic)}
count = 0
for fName in fieldNames:
if fName not in fieldsExisting.keys(): # Check if field is not already present in the Mosaic Dataset attribute table
arcpy.AddField_management(inMosaic, fName, "TEXT", field_alias=aliasNames[count], field_length=10) # If not, add field
arcpy.AddMessage("{0} field has been added to Mosaic Dataset!".format(fName))
count += 1 # Increase counter for each loop. This is necessary for iterating through the aliasNames list
arcpy.management.CalculateField(inMosaic,"YEAR", yearfield)
arcpy.management.CalculateField(inMosaic,"INST_ABBR", installfield)
arcpy.management.CalculateField(inMosaic,"SITE_ABBR", sitefield)
arcpy.management.CalculateField(inMosaic,"GroupName", groupnamefield)
arcpy.management.CalculateField(inMosaic,"ProductName", productnamefield)
Solved! Go to Solution.
Here's the problem:
arcpy.management.CalculateField(inTable, "TextField", "some text")
# the tool will try to evaluate the expression. Internally, it will try to run this:
def expression():
return some text
# this of course fails, because it's not valid syntax, and because it evaluates _some_ and _text_ as variales, and they aren't defined
# to fix that, you have to correctly escape your strings:
arcpy.management.CalculateField(inTable, "TextField", "'some text'")
def expression():
return 'some text'
Here's how you can fix it:
arcpy.management.CalculateField(inMosaic, "YEAR", "'{}'".format(yearfield))
Code formatting ... the Community Version - Esri Community
can you refer to specific lines?
Any of the calculate field code:
arcpy.management.CalculateField(inMosaic,"YEAR", yearfield)
I have yearfield = getparameterastext and the script properties has it set to string
Here's the problem:
arcpy.management.CalculateField(inTable, "TextField", "some text")
# the tool will try to evaluate the expression. Internally, it will try to run this:
def expression():
return some text
# this of course fails, because it's not valid syntax, and because it evaluates _some_ and _text_ as variales, and they aren't defined
# to fix that, you have to correctly escape your strings:
arcpy.management.CalculateField(inTable, "TextField", "'some text'")
def expression():
return 'some text'
Here's how you can fix it:
arcpy.management.CalculateField(inMosaic, "YEAR", "'{}'".format(yearfield))
Thank you for your help! It worked perfectly.