I am looking for assistance to rewrite my VB codeblock into Python. Im on Win7, Arc10.2 and Python 2.7 The code below works fine for me, I am simply looking to port it to Python so i can progress down that path.

    # import modules
    import arcpy
    # Set the workspace environment
    arcpy.env.workspace = 'C:/data/Library/Peter/SPZNT_02.gdb'
    ws = arcpy.env.workspace
    print "Env.workspace set to: '" +ws+ "'" '\n'

    ## Variables specific to "What needs doing" only ##

    # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
    # The following inputs are layers or table views: "Reduction_04"
    InField = "[OBJECTID]"  # Change to Field Name that will replace blanks
    Output = "[Test]"       # Change to the field being calculated to replace its blanks

    inFeatures = "Reduction_04"
    fieldName = "Test"
    expression = "Output"
    code = "VB"
    codeblock = "InField = [OBJECTID]\n \
                 Output = [Test]\n \
                 OutVarType = VarType(Output)\n \
                 If OutVarType = vbNull then\n \
                 Output = InField\n \
                 ElseIf OutVarType >= vbInteger and OutVarType <= vbDouble Then\n \
                 if Output = 0 Then\n \
                 Output = InField\n \
                 End If\n \
                 End If"

    # Execute CalculateField
    arcpy.CalculateField_management(inFeatures, fieldName, expression, code, codeblock)
  • The feature class is in a FGDB. – user1995458 Apr 14 '15 at 1:38
  • What have you tried and where are you stuck? Can you edit your question to include your Python code so far, please? – PolyGeo Apr 14 '15 at 2:06
  • 1
    Could you explain what it's trying to do, in pseudocode preferably.. it's quite cryptic trying to read the code block like that. You're using an enum as a numeric (int), which isn't incorrect, but not good practice (you can't expect the enum to be in the same order on the next release, it probably will be, but it's not guaranteed) It looks like you're trying to do IsNumeric([Test]) but I can't be sure what you're trying to achieve in the end. – Michael Stimson Apr 14 '15 at 3:15
  • Apologies, I should have described it better. I am looking for a field that is either Null or 0 and if found I want to populate it with another field's value. (Thanks Jason, you deciphered my question successfully) – user1995458 Apr 15 '15 at 1:22
  • @PolyGeo I have updated the question to include the Python code so far. Jason's answer worked for me with a couple of small tweaks so I will post it. – user1995458 Apr 15 '15 at 5:04

Since it looks like you are running this in a separate arcpy script (as opposed to inside the Field Calculator), there may not be a need to use Code Blocks. Maybe better to use an UpdateCursor...

From what I can tell from the code, you are testing to see if the field you need to fill is either Null or 0, and if it is, fill the field with the value from the InField... try this...

inputField = "OBJECTID"
outputField = "Test"

fc = "C:/MyPath/MyFGDB/MyDataset/Reduction_04"
fields = [inputField, outputField]

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if (row[1] or 0) == 0:
            row[1] = row[0]

The " if (row[1] or 0) == 0: " code: "row[1] or 0" will return either the value of the outputField, or if that value is None (null), then it will return 0...

  • Yes, tried and works just fine with minor changes as below. – user1995458 Apr 15 '15 at 5:06
  • arcpy.env.workspace = 'C:/data/Library/Peter/SPZNT_02.gdb' fc = 'Reduction_04' inputField = "OBJECTID" outputField = "Test" – user1995458 Apr 15 '15 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.