2

I am familiar with using the ArcPy Calculate Field tool in Python but was wondering how this is done in a stand alone Python script with a .csv. My ArcPy Snippet looks like this. I assume it's a basic procedure but I couldn't find any previous questions. Feel free to direct me to a similar question.

codeblock = """def Reclass(PA,dist):
                    if PA=="M" and dist==1:
                        return "MM"
                    elif PA=="M" and dist==0:
                         return "MW""""

arcpy.CalculateField_management(fc, field="NEW", expression="Reclass(!Field1!,!Field2!)", expression_type="PYTHON_9.3", code_block=codeblock)
2
  • I'm a bit confused do you mean how to programmatically run calculate field so that you can access a CSV from outside ArcGIS. For example run Arcpy to access external CSV and bring it into ArcGIS then, perhaps in memory, convert the CSV to a dBASE or geodatabase, run calculate fields and export to CSV?
    – spk578
    Commented Aug 24, 2016 at 13:47
  • Please avoid cross-posting: stackoverflow.com/q/39056544/1446289. More details on SE policy on cross-posting here: meta.stackexchange.com/q/64068
    – Aaron
    Commented Aug 24, 2016 at 14:23

2 Answers 2

1

It won't work as a CSV is read only. If you want to update your data with field calculations then you need to convert your CSV into a proper data table such as a dBase file or geodatabase table.

1

You should be able to do this by utilising the scratch workspace and converting your CSV out to a geodatabase table e.g. below

import arcpy, os
#Set path to temp. geodatabase
arcpy.env.scratchWorkspace = r"D:\GIS\database"
#Path to newly created gdb
gdb = arcpy.env.scratchGDB

Then you could use CopyFeatures to add the CSV to the scratch gdb

#Copyfeatures from CSV to gdb
arcpy.CopyFeatures_management(r"c:\temp\foo.csv", gdb)

And from there run the CalculateField tool and export to the scratch gdb using the arcpy.TableToTable_conversion() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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