Tag Info

New answers tagged

4

Put a u in front of the string so the interpreter knows it's Unicode. myCalc(!Epulettipus!, u"Lakóépület")


3

You don't 'Dim' variables in python, you just declare and assign them. That said, your basic route is to set the parser to python and check Show Codeblock. In the Pre-Logic Script Code box, enter your function like this: def DoThis(fld): val = 0 if fld <> 'a certain string': val = # do your calculation here return val In your ...


-2

I'm a long time ArcGIS user but find that the QGIS field calculator is a lot more intuitive & provides options for dragging & dropping fieldnames, expressions & operators. If you are a programmer, then you'll probably find it a lot easier and you'll find it builds your knowledge for using the expression builder in ArcGIS.


0

When you define your concat_fields function there should be no exclamation marks used because you are at that stage dealing with Python variable names. def concat_fields(AREA_HI, AREA_LO): if AREA_HI.strip() == "" or AREA_LO.strip() == "": return "" else: return AREA_HI + AREA_LO However, be sure to leave the exclamation marks on ...


0

It's quite a hassle but you could write a Feature Class extension in the geodatabase. In the create event you just update the Service number field. Read more on feature class extensions: http://resources.arcgis.com/en/help/arcobjects-net/conceptualHelp/#/Creating_class_extensions/000100000201000000/ I don't say it is the only way, but it is very reliable ...


3

Based on your variable, you just need to make sure variables and strings are not confused field_1="!field_name1!" field_2="!field_name2!" arcpy.CalculateField_management("Layer", field_0, field_1 + " / " + field_2, "PYTHON_9.3") note that if you have the strings as variables without the "!", it is nicer to use format() field_1="field_name1" ...


1

Have a search in help for the subject "Performance tips for joining data" it offers advice on improving join performance. Your code does not indicate you have added an attribute index which can often improve performance.


2

You are missing the wrapping exclamation marks, try this: arcpy.CalculateField_management("Layer", field_0, '!' + field_1 + '!/!' + field_2 + '!', "PYTHON_9.3")


2

You have embedded the string variable within a string so python sees it as a string. I would suggest you make you code something like: field_0="field_name0" calcString = "!field_name1! / !field_name2!" arcpy.CalculateField_management("Layer", field_0, calcString', "PYTHON_9.3")


4

Apparently RefName never equals your comparison strings. All constant strings includes German characters ä or ü, but you don't use the "u" constant string prefix. Depending on the incoming string's format the comparison might not be what you expekt. You should write if RefName==u"Flächen für die Landwirdshaft" and make sure that RefName also is a correct ...


3

Here is a field calculator method that incorporates itertools.takewhile. While ian's solution writes the new values to a text field, this is suited for writing to a numeric field. import itertools def convert(x): try: return int("".join(itertools.takewhile(str.isdigit, str(x)))) except: pass convert(!OriginalString!)


6

Here's a go at it. Use Python as the parser and check show Codeblock. Enter this in the top Pre-Logic Script Code box: def getints(field): integers = [] for char in field: try: value = int(char) integers.append(str(value)) except ValueError: break return "".join(integers) And put ...


2

!shape.length3D! does work. I had to make sure that I selected the PYTHON_9.3 Expression type in the Calculate field Options dialog. (I'm running 10.2.)


0

ModelBuilder only allows one iterator per model. I would integrate a model within a model in order to iterate over multiple tables.


3

Similar to @underdark, but in my installation, i had to use CASE WHEN "COLUMN1" IS NOT NULL THEN 'Town' ELSE 0 END and be sure the correct update field is checked, as you mentioned. when i attempted to set "COLUMN2" to some value with the CASE comparison, it will only evaluate true or false (0 or 1), rather than actually updating with ...


5

Note that "Town" refers to a column called Town, while 'Town' is the string that you actually want to assign. Therefore: (Updated to correct the then and else parts. Thanks fluidmotion for pointing out the issues.) CASE WHEN "COLUMN1" IS NOT NULL THEN 'Town' ELSE 0 END


0

Okay, I found my own solution not as straight forward as phloem's but it works: Make a new table which contains a field with values identical to those of a field in the original table but in reverse order (back to front) Copy this new table (so you can repeat the procedure) Join the field which contains the values you want to calculate the decreasing ...


0

Okay, I found my own solution: Make a new table which contains a field with values identical to those of a field in the original table and another in which these values are in reverse order Copy this new table Join the field(s) you wish to flip to the new table, using the field which is reversed in the new table as the output join field, and the reversed ...


2

Assuming that the numbering of your rows always starts at 1 and proceeds with increments of 1 to a different number each time I think your model/code needs to do this: Use GetCount to count the number of rows (numRows) Use Calculate Field to do (numRows - ID) + 1


0

You can do this easily by using the sort tool. This tool reorders, in ascending or descending order, records in a feature class or table based on field values. The reordered result is copied to a new dataset.


1

Create a new field and put field type is text after that you should use field calculator the value by simply select all "nulls" and change it in to "0".


0

A bit of a hack-ish workaround, but you can typecast to str and use string.replace(",", ".") then cast back to float. Would love to comment on the other workaround answer instead, still need 2 more points... def giveResult(floatValue): if floatValue>0: a=(1000,5000,10000,15000,20000) for i in range(len(a)): product = str(a[i]/1000) ...


2

Ok, this is clearly an ArcGIS bug (as we've deduced in the comments on @ian's answer). Here's my attempt at a workaround, which assumes there can be either zero or one commas in the number (and therefore, as a result of the bug, you effectively pass the function either a single integer or a tuple of two integers): def giveResult(*args): floatValue = ...


2

I can see from your field name !DEAD_VOL_PER_HA_SPP1_125! that the field you are calculating is likely a float or integer type field. Assuming this is true, you cannot write a space or '' into a number-based field (e.g. return ''). Valid values for number-based fields include None or a numeric value. You will need to either create a new text field to ...


1

Edited: this expression relies on knowing the total (23) beforehand, but would be possible to calculate in the script, if needed. For the example series in the comments (3, 6, 5, 9) this expression returns 23, 20, 14, 9. total = 23 prev = 0 def cumsum(inc): global total global prev total -= prev prev = inc return total


0

Note that in your case you could also select by attribute "TESTFIELD"='0' (select by attribute), then update only those values with field calculator. If you have a vera large table and very few cases with "TESTFIELD"='0' , it could be faster.


3

The code should work, just add the colon after if statement and proper indentation: def RemoveNULLS(x, v): if x == '0': return v else: return x RemoveNULLS( !TESTFIELD!, !TESTFIELDBEREGNER!) Be careful with what data type used for the x (if it is an integer, you cannot use '0', should be 0).


1

You need a codeblock in your field calculator. The example below is with a Python parser total = 0 def cumsum(inc): global total total+=inc return total The first line initialize a variable to 0, then you define a function called cumsum with one argument. for the incrementation, you want your variable to be "global" that will keep incrementing the ...


0

You can compute the area using a right-click on a field > calculate geometry. Then you summarize your attribute table using the building name as a case field : you can then ask for the sum of the area. Finally, use a table join (join by attribute) the have the total area of each building, which you can copy if needed in a new field. If you don't have an ...


2

Lets start with the field calculator. it should look like these 2 screenshots. When I calculate fields inside of an Update cursor (update_cursor) it looks more like this. Here I have a SearchCursor (row is set to searchCursor.next) and an update Cursor (row2 is set to update_cursor.next). This is actually slightly different since I am using values in a ...


5

Here's a way to replace all the nulls with zeroes: import arcpy with arcpy.da.UpdateCursor(r"...gdb\test.gdb\test",["A", "B", "C"]) as cursor: for row in cursor: a_value = row[0] if row[0] else 0 # Use 0 when "A" is falsy b_value = row[1] if row[1] else 0 # Use 0 when "B" is falsy row[2] = a_value + b_value ...


3

Disabling the "on-the-fly" projection setting should yield you the correct results when performing analytics via the Field Calculator: Project > Project properties > CRS This bug has been known in early versions of QGIS but has mostly been resolved since QGIS 2.1.


6

If all your data matches the format you posted, and all the numbers you want are in between the two dashes in the text (and those are the only dashes), you can use the split() method. Here's the code: def extractText (yourTextField): partialText = yourTextField.split("-") return partialText[1] That will split the input into 3 parts: the parts ...


9

What you are looking at is an Advanced Field Calculation. It's a little confusing because you're kind of referencing it backwards. If you right-click on a field in an attribute table and select the field calculator, you'll notice an option in the field calculator window to change the parser to Python as well as a check box named 'Show Code Block'. When ...


0

I wanted to provide another alternative to my previous answer I am more of a fan of using the python interactive window and an UpdateCursor for most of my attribute table changes. When I wrote my above answer, I wrote it in the interactive window and translated it to the field calculator. My interactive code was: replaceList = ["DRG", "2014"] ...


1

I am assuming you mean field value that needs changed and not your layer name since you mentioned Field Calculator Same logic could be applied in a script for actual layer names too though Ok.. I understand you and have it down. (1) Make sure your parser is marked for python. (2) In the Pre-Logic Script Code: def customReplace(fieldValue): repList = ...


2

I am not completely sure to understand the aim of your script, but here are some comments def customReplace(fieldV): # you must indent after your def repList = ['-', ' ', '.'] #remove the ; rep = '_' b=fieldV for old in repList: #I guess you want to replace the values in replist b = b.replace(old, rep) #here you need return, not ...


0

I'm not sure what you're trying to do exactly. Is Layer supposed to be a feature class or shapefile, or a field? One, all the lines in the function should be indented. Two, you can't have a semicolon end a line. Three, Layer is probably a reserved word, don't use it. Four, repList isn't in Layer; that loop will either error out or give you unexpected ...



Top 50 recent answers are included