jdrvar;330609 wrote:Well this is nothing like a Field Calculation, so of course the !TextString! won't work. I did not know you were going to this kind of script. Just use the updatecursor itself to do this.FieldName = arcpy.GetParameterAsText(0) cursor = arcpy.da.UpdateCursor(History, [FieldName]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursorThe input parameter would be any text field name. So choose "TextString" in that parameter. Or forget the parameter and hard code the field name.cursor = arcpy.da.UpdateCursor(History, ["TextString"]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursor I suppose I should have been more specific. Using your suggestion works! I just need to refresh to display the ().arcpy.RefreshActiveView()
jdrvar;330609 wrote:Well this is nothing like a Field Calculation, so of course the !TextString! won't work. I did not know you were going to this kind of script. Just use the updatecursor itself to do this.FieldName = arcpy.GetParameterAsText(0) cursor = arcpy.da.UpdateCursor(History, [FieldName]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursorThe input parameter would be any text field name. So choose "TextString" in that parameter. Or forget the parameter and hard code the field name.cursor = arcpy.da.UpdateCursor(History, ["TextString"]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursor
FieldName = arcpy.GetParameterAsText(0) cursor = arcpy.da.UpdateCursor(History, [FieldName]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursor
cursor = arcpy.da.UpdateCursor(History, ["TextString"]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursor
arcpy.RefreshActiveView()
jdrvar;329266 wrote:I changed the text to match my field name. My field name is TextString. So, my line of script reads: "(" + !TextString! + ")"QUOTE]addParentheses = "(" + !TextString! + ")" returns a Syntax Error. I know the field name and type are correct, therefore, believed the expression to be correct. That left the concatenation of parentheses. But, the concatenation worked within Field Calculator. Then, I went back reread the posts and tried adding a second variable; expression = (!TextString!). Which also created a Syntax Error. So, I removed the exclamation points from the addParentheses variable. Removing the exclamation points, the script runs, and replaces existing text with parentheses. But, I want to concatenate the existing text with parentheses. Any suggestions? TextString = arcpy.GetParameterAsText(0) addParentheses = "(" + TextString + ")" cursor = arcpy.da.UpdateCursor(History, ["TextString"]) for row in cursor: row[0] = addParentheses cursor.updateRow(row) del row del cursor Well this is nothing like a Field Calculation, so of course the !TextString! won't work. I did not know you were going to this kind of script. Just use the updatecursor itself to do this.FieldName = arcpy.GetParameterAsText(0) cursor = arcpy.da.UpdateCursor(History, [FieldName]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursorThe input parameter would be any text field name. So choose "TextString" in that parameter. Or forget the parameter and hard code the field name.cursor = arcpy.da.UpdateCursor(History, ["TextString"]) for row in cursor: row[0] = "(" + row[0] + ")" cursor.updateRow(row) del row del cursor
jdrvar;329266 wrote:I changed the text to match my field name. My field name is TextString. So, my line of script reads: "(" + !TextString! + ")"QUOTE]addParentheses = "(" + !TextString! + ")" returns a Syntax Error. I know the field name and type are correct, therefore, believed the expression to be correct. That left the concatenation of parentheses. But, the concatenation worked within Field Calculator. Then, I went back reread the posts and tried adding a second variable; expression = (!TextString!). Which also created a Syntax Error. So, I removed the exclamation points from the addParentheses variable. Removing the exclamation points, the script runs, and replaces existing text with parentheses. But, I want to concatenate the existing text with parentheses. Any suggestions? TextString = arcpy.GetParameterAsText(0) addParentheses = "(" + TextString + ")" cursor = arcpy.da.UpdateCursor(History, ["TextString"]) for row in cursor: row[0] = addParentheses cursor.updateRow(row) del row del cursor
TextString = arcpy.GetParameterAsText(0) addParentheses = "(" + TextString + ")" cursor = arcpy.da.UpdateCursor(History, ["TextString"]) for row in cursor: row[0] = addParentheses cursor.updateRow(row) del row del cursor
I am running a similiar script to copy & paste annotations, which is why I posted in this thread. The copy & paste part of my script works fine. I have two annotation Feature Classes, but only want the parentheses added to one Annotation Feature Class. Which, is why I attempted to specify the Feature Class to add the parentheses. I probably should have set up a conditional statement.Your suggestion of "(" + !TextString! + ")" works using Field Calculator. Parentheses are added to selected values within the TextString field.
def AddParentheses(TextString): Output = TextString if Output[0] != "(": Output = "(" + Output + ")" return Output
I have no idea what you think you were doing in the second calculation, but that version definitely should not work. Aren't you just using a single feature class for the anno? If two feature classes are involved you never mentioned that.Screen shot the table and the field calculator dialog set up you used with my original expression. I want to see exactly what you have and what you did.Use the Field Calculator field list to build the expression and to insert the field name in the calculation. Quotes should be fine, but so should single quotes (only for Python, not for VB Script).
I changed the text to match my field name. My field name is TextString. So, my line of script reads: "(" + !TextString! + ")"But, a Syntax Error is returned. Changing the outer double quotes to single quotes eliminates the syntax error, yet does not add the parentheses. I have a couple of Annotation Feature Classes with the field TextString. Then, I attempted to specify which Feature Class to apply the parentheses. '(" + (History)!TextString! + ")'The script does not have any errors. Yet, does not add the parentheses.
Than the text should be calculated to a simple Python calculation of:"(" + !AnnoText! + ")"where you are calculating the annotation text field and that text field name is AnnoText. Change AnnoText to match your actual annotation text field name.The same simple calculation in VB Script is:"(" & [AnnoText] & ")"
The string is not working. I suspect that is because I asked the wrong question. I have an Annotation Feature Class that is displaying the information as integers within ArcMap, but the field type is a string.
In python to make an integer variable into a string with parentheses added around it you would do something like:parenInt = "(" + str(myInt) + ")"where myInt is an integer value variable and parenInt is a string of the interger surrounded by parantheses.
All,I am writing a similiar script. I need the output to have parentheses added around an integer. For example, instead of 31, I need the output to be (31). Where can I locate information on how to add parentheses?
It sounds to me like you are expecting Copy Features to do something it won't do. It won't append features to an existing feature class, it creates a new feature class. To append to an existing feature class and keep any previously existing features you have to use Append.If you actually do want to overwrite an existing feature class with a new feature class that uses the same name you cannot use the environment overwrite setting in a Python script to do that. That setting does not work in a Python script, it only works in Model Builder. You have to explicitly use the Delete tool to delete an existing feature class and then you can write to the same name the second time. This is how it always works. I have to run a script once without the Delete tool to just generate the outputs during the first run. Then before I can run it any more times I first have to add the Delete tool to the code and then I can run it again.
Richard,The result is the same. The script works, the selected annotation is copied from pa to oa. But, when the script is run the second time, the first copy is nullified, and the annotation(s) revert back from oa to pa. It works like editing and not saving edits. The tricky part seems to be copying & pasting into an existing Feature Class and having the Annotations saved into the Feature Class. I have also tried the Copy_management(input, output), but the statement does not like the existing oa Feature Class. Any further suggestions on how to get the copy & paste to "save"?Mark,Your suggestion of copying & pasting to a new Feature Class works. But, I want to use the two existing Annotation Feature Classes within ArcMap.
Try:arcpy.CopyFeatures_management(pa, oa.dataSource)pa should be the layer, because it has a selection, but dataSource should get the feature class and path for the output.
Richard,I have tested each of the suggestions multiple times and my results were unsuccessful.arcpy.CopyFeatures_management(pa, oa) approach returns the Error 840: The value is not a Feature Class.arcpy.CopyFeatures_management(pa.name, oa.name approach runs but does not work.But, arcpy.env.workspace = "C:\Working\MyData\Python.gdb"arcpy.CopyFeatures_management("PropertyAnno", "OwnerAnno") works. An annotation is selected and using the Select Features tool, the script runs, and copies and pastes the annotation from PropertyAnno into OwnerAnno.The issue is with:pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0]oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0]They are defined as Layers, but the output requires a Feature Class. Commenting those out, the script works outside of an edit session. But, when the script is run multiple times, the previous copy does not save. How can the copy be saved?Mark, I am currently testing your suggestion.
It doesn't work because you are telling the CopyFeatures_management to use the literal strings "pa" and "oa", which are not the names of any layers. Unquote those variables and possibly use the .name property as you did in the messages. So try either:arcpy.CopyFeatures_management(pa, oa)or tryarcpy.CopyFeatures_management(pa.name, oa.name)
I am confused as to why the script does not work. I believe something is wrong my CopyFeatures_management statement. import arcpy arcpy.env.workSpace = "C:\Working\MyData\Python.gdb" arcpy.env.overwriteOutput = 'True' mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd)[0] pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0] oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0] arcpy.AddMessage(pa.name) arcpy.AddMessage(oa.name) arcpy.CopyFeatures_management("pa", "oa")Yet, if I write a basic script the CopyFeatures_management works. I only want to copy selected features & paste into an existing Feature Class, not create a new Feature Class. import arcpytry: arcpy.env.workspace = "C:\Working\MyData\Python.gdb" arcpy.CopyFeatures_management("HomeValue", "Test")except: print arcpy.GetMessages()
import arcpy arcpy.env.workSpace = "C:\Working\MyData\Python.gdb" arcpy.env.overwriteOutput = 'True' mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd)[0] pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0] oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0] arcpy.AddMessage(pa.name) arcpy.AddMessage(oa.name) arcpy.CopyFeatures_management("pa", "oa")
pa = arcpy.mapping.ListLayers(mxd, "PropertyAnno", df)[0] # pa is the input: it is a layer in arcMap. # Copy Features accepts a layer as input, # so you can have selected or query defined subsets of the FC features oa = arcpy.mapping.ListLayers(mxd, "OwnerAnno", df)[0] # oa is supposed to be the output: it is a layer in arcMap. # Copy Features cannot accept a layer as output; it must write to a fc on disc # since the oa layer is in your open ArcMap session, the underlying FC is locked # so you cannot write to the underlyng FC either fc = r"C:\Working\MyData\Python.gdb\AnAnnotationFC" # fc is an annotation FC in the same gdb, but is NOT in the open mxd # try writing to that arcpy.CopyFeatures_management(pa, fc)
The feature layer at its core is a feature class, but the layer itself is on top of that and is a Desktop display wrapper that defines its output location as a layer designed for placement in a map dataframe, not as a feature class file in a directory. This geoprocessing tool cannot use the layer wrapper that hides the feature class information from the tool.That is why when you interactively complete the tool you cannot drag a layer into the output text box of the CopyFeatures tool or type a layer name into the output of the tool. You have to navigate directories to fill in that part of the tool dialog with a full feature class path or you must only connect raw feature class variables or tool outputs that store direct pointers to a feature class path. Layers are only an indirect pointer to a feature class, not a direct pointer.Although many people use the term layer indiscriminately to refer to both what you see in the TOC of a Desktop map and the underlying data in ArcCatalog, technically the term is actually only correctly used to refer to what you see in a Desktop map TOC. The data stored on disk seen in ArcCatalog is only a feature class that has its own independent existence even if it was never used to create a layer in a Desktop map TOC. Likewise, layers can exist without a feature class (when a layer has a red exclamation mark due to a lost data connection it is still a layer even though it has no feature class and virtually no real functionality).
Mark,How is oa not a Feature Class? ArcCatalog and ArcMap list the Data Type as a File Geodatabase Feature Class.
The output of CopyFeatures is a feature class, not a layer.oa is a layer, not a Feature Class.
What do you mean by "cut & paste annotations"?Copy Features respects feature selections on layers....
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.