Hot answers tagged

6

Try passing your variable to your string: var_a = 6.2 arcpy.CalculateField_management (in_table, in_field,"[FIELD_A]*100/{}".format(var_a))


6

You can use the following code which connects the attributeValueChanged event to a function we can define which inserts the results of the $now expression. Highlight your layer and copy/paste the following into the Python Console: layer = qgis.utils.iface.activeLayer() def update(): field = layer.fieldNameIndex('mod') e = QgsExpression( " $now " ) ...


6

I'm not sure you can with field calculator, but you can use a virtual layer with a query like : SELECT * , (SELECT count(*) FROM your_layer AS b WHERE a.length >= b.length) AS length_position FROM your_layer AS a ORDER BY a.length


5

So, I've coded a plugin (Sort and Number) to solve your issue. It allows you to select up to 3 fields and order your attribute table according to these fields. Then, it numbers the attribute table in a new field (named "order" by default), starting from 1.


5

"Solution" with 2 sorted fields (ascending): mxd = arcpy.mapping.MapDocument("CURRENT") lr=arcpy.mapping.ListLayers(mxd)[0] tbl=arcpy.da.TableToNumPyArray(lr,("oid","A","B")) bs=sorted(tbl, key=lambda x: (x[1], x[2])) def sortSeq(fid,a,b): for i,ent in enumerate(bs): if ent[0]==fid: return i -------------------------------------- sortSeq( !OID!, !A!, ...


5

Even though this question can be considered off topic I though I would make a suggestion. Try using the Python re module in the Field Calculator. Here is a simple example used in the ArcMap field calculator. Pre-Logic Script Code import re def repChar(strg) regexChars = '[!@$*#]' line = re.sub(regexChars,'',x) return line Field ...


4

You can use the \W (upper case W) to match any non-alphanumeric characters. If using regexp_replace() it seems you need to escape the backslash, so it becomes \\W (thanks, Joseph!) For example regexp_replace( '=+&hello%%£world','\\W+','_') returns _hello_world If using the python equivalent function re.sub(), you probably won't need to escape ...


4

This is a two step process, and as a result, the Field Calculator is not well suited to it. It is better to run this in a standalone script. However, it can be done in the field calculator, provided you use a trick. You do need to use a cursor to load all of the values to a global dictionary from a sorted list, but only during the calculation of the first ...


3

This will convert ALL last 2 characters to uppercase, even if these characters are not directions (Nw, Sw,...) : concat( substr( title( "fieldname"), 1, length( "fieldname")-2), upper(substr("fieldname", length( "fieldname")-1, 2)) )


2

According to http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/expressions.html the member function QgsExpression.prepare() is not mandatory but makes the execution faster. I would recommend to comment out the line e.prepare(layer.pendingFields()) and see what happens. And as far as I see, an exp.prepare(...) is missing directly behind the ...


2

There were some records that were blank which caused an error. This code works: def getSufDir( prop_location ): if prop_location is None: return None else: return prop_location.strip().split(" ")[-1]


2

Since your working in arcpy and doing a simple data transfer, use a search cursor to load your Spatial Join count results as the value in a dictionary associated with the FID key and then use and update cursor to read the FID from the current feature being updated and lookup the count from the dictionary to post it. It takes a few more lines of code to set ...


2

Your single line !file!.replace(" ","") can be added to by including more .replace() !file!.replace(" ","").replace("?","").replace(",","").replace(chr(33),"") and so on. You may need to escape some of the characters, or use their chr() code if the field calculator doesn't like them. chr(33) in my example replaces the !. Chr codes can be retrieved ...


1

Try putting your number "01" into a second set of quotes "'01'" (double-quote then single-quote or the other way around) arcpy.CalculateField_management("Table1","Field1","'01'","PYTHON","#")


1

I was able to reproduce this effect, although I had to end up using the code: !shape.area@squaremeters! I got the following results in my table, with both fields being identical like you stated: Reading into the help guide, it looks like it could be an issue with the fact that the "Calculate Geometry" tool lets you utilize the coordinate system that ...


1

It's not pretty, but ''.join([x for x in list(!file!) if x not in ["!","@","$"]]) should remove any single character you specify in the list, and return the resulting characters as a string (the "etc" in your example wouldn't work, however).



Only top voted, non community-wiki answers of a minimum length are eligible