Tag Info

Hot answers tagged

7

In addition to @DWynne's answer, I'll add that there is a built-in python function that does exactly this (located a little bit further down the page on DWynne's link). It's called zfill and according to the documentation: Returns the numeric string left filled with zeros in a string of length width. A sign prefix is handled correctly. The original ...


5

Export your table to a text format. Grab the header row and paste it to a new file. Delete irrelevant column headings (could also turn them off prior to export to avoid this step), and then use find/replace functions to change the delimiting characters to proper syntax for the formula (ie quotes around field names, a plus sign in the middle). Copy and paste ...


4

Out of the box, field calculator does not support spatial joins across feature layers. But, if you have a look at NathanW's post on the function editor for qgis expressions you will be able to make out that we can script our own data interaction. The following script will allow you to express what you're after. It works by iterating through all features ...


4

Actually, you could do this without a function. Try using str.rjust. An expression of !photoStr!.rjust(5, '0') will return a string with padded 0's to the left of your number string. '1' becomes '00001' '12' becomes '00012' '123' becomes '00123' '1234' becomes '01234'


3

Using the Python parser in the field calculator: Pre-logic script code: def convert(x): a = x.split(":") return a[0] + ":\Engineering" + a[1] convert(!text_field!) You can also use an UpdateCursor to do this import arcpy fc = r'path\to\your\fc' with arcpy.da.UpdateCursor(fc, "your_field") as cursor: for row in cursor: if row[0] ...


3

The ultimate result of this calculation is a boolean typed value (i.e., True or False). ArcMap does not have a boolean type built in, but you can simulate it in one of two ways: If you are using a geodatabase, best practice would be to store the result of this calculation using an Attribute Domain that you define to your needs. If that's overkill, you ...


3

Seems you're correct in thinking there is a problem with the 'calculate' variable. i believe you would want something like arcpy.CalculateField_management(layerforNewField, newfieldname, '"!{}!"'.format(oldfield), "PYTHON_9.3") otherwise, by setting calculate = '!oldfield!' the code is trying to insert values from the column 'oldfield', which i'm ...


2

You can use the field calculator to update the column removing unwanted chars: replace( "mycolumn", '#F','') You can also use this directly in an label expression, if you do not want to delete the char from the record. You have a long list of string function for you convenience.


2

Use the ModelBuilders' Calculate Field to insert a python code like this: Expression: calculate(!Field1!, !Field2!) And a code block of: previousValue1 = "" previousValue2 = "" previousUnchangedValue1 = "" def calculate(field1, field2): global previousValue1 global previousValue2 global previousUnchangedValue1 #First item in list if not ...


2

you could use some Python scripting, I assume that the field "resultfield" exists and that you can identify the fields to sum based on one common string (i.e. "score") : import arcpy allfields = arcpy.ListFields("featureclassname") scorefields = [x.name for x in allfields if x.name.find("score")>-1] with arcpy.da.UpdateCursor("featureclassname", ...


2

Based on where you are, use the field calculator. Select Python as the parser and then in the formula block, put: ','.join([Polys].split(',').sort()) Note that if you are using a personal or file geodatabase, you would need to put !Polys!. However, there are simpler alternatives if you wish to do the whole thing from the beginning again.


2

as written, the function returns a string with 'Nearby:' removed only if the 'Nearby:' test is true (if 'nearby:' exists within the field string. If the test is false, the function is set to 'pass', which returns no value (deleting the original value in the process) to remedy, a value should be passed for both cases - true and false - something like def ...


2

You could simply use the Coalesce function which returns the first non-NULL value from the given columns (fields). Insert this expression into the Field Calculator as a new field: coalesce( "Field_1", "Field_2", "Field_3" ) Below is a simple example: If a feature has columns with multiple values (eg. 1, NULL, 3), then you may need to include ...


1

I really like using feature layers and selection for this kind of thing. Below I find all my relevant fields, then try selecting Nulls/''. If there's a selection, I field calculate. I haven't tested the script, so it may not be perfect. def removeNumericNulls (inFeatureClass): import arcpy #Relevant field types fieldTypes = ["Double", ...


1

Assuming "Nearby:" is at the beginning of the line (and never in the middle), you can simplify this logic with a one-liner, without the need of a codeblock: str(!Street1!).replace('Nearby:', '').lstrip()


1

I would assume you're using this in the advanced field calculator (show codeblock)... In this case "Tertiary" would never be hit as you're getting the select case of the uppercase and that word contains lower case characters. In this case a=10 at the start and none of the Case in the block are ever hit. As to why none of these cases are ever hit depends on ...


1

There is a much simpler way using a Dictionary object. To be accessible/updateable for each iteration it must be outside the scope of the function (global) otherwise it's reset for each calculation. preDict = {} def CalcField3(Field1,Field2): global preDict # tell python this isn't a new variable but the other one if preDict.has_key(Field2): # if ...


1

If you want this as a python script, then you could do something with an update cursor... inputData = "" #Needs to be the full path codeField = "" updateField = "" with arcpy.da.UpdateCursor(inputData, [codeField, updateField]) as updateRows: for row in updateRows: if row[0] == "TestValue1": row[1] = "NewValue" elif #... etc ...


1

In the advanced field calculation be sure to select python as the syntax: The code (to copy and paste): def FormatThis(number): AsString = str(int(number)) # just the whole numbers Dec = number-int(number) # extract the decimals G3 = [AsString[::-1][i:i+3][::-1] for i in range(0, len(AsString), 3)][::-1] return ','.join(G3) + ...


1

Assuming that you are looking for positive numbers, as it looks like you are extracting address elements. Run a field calculate on Name_Num field. Put this in the pre-logic Script Code (make sure Parser is Python): def extract_from_number_to_end(input_string): output = input_string # return input if no number found for char in input_string: ...


1

Here's a way to implement Chris W's answer without the need for ArcGIS. (It does require gdal/ogr, free and open source.) When gdal/ogr is installed, run the code below from a command terminal (mac/linux/windows). To get the list of fields Chris mentions: ogr2ogr -f csv fields.csv union.shp -where "0=1" Search + replace that text to create the math ...


1

Field calculator (Python) def TotalTHem(fid, layerName,wildcard): mxd = arcpy.mapping.MapDocument("CURRENT") lr=arcpy.mapping.ListLayers(mxd, layerName )[0] destFields=arcpy.ListFields(lr) dNames=[row.name.upper() for row in destFields] list2add=[] for fname in dNames: if wildcard in fname:list2add.append(fname) with ...


1

I don't have experience coding but i modified one of the esri resources examples and it worked for me... # Name: CalculateField_COORDINATES.py # Description: Use CalculateField to calculate start and end points of a line # Import system modules import arcpy try: # Set environment settings arcpy.env.workspace = "D:\SIG_MPIOS\MPIOS14.mdb" # Set local ...



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