Select to view content in your preferred language

Add and Calculate Field within a python script

1608
3
Jump to solution
05-09-2013 02:11 AM
ChristianJoyner
New Contributor
I'm trying to build a tool in python that will add a field and calcuate the field that was just added to the script. I'm getting error 000732: Input Table: Dataset test.txt does not exist or is not supported. I thought I had the script set up to have the user enter the table input from the current open display. Please help I'm still faily new to python. Thanks. The tool will be added to a current model in model builder.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ArkadiuszMatoszka
Occasional Contributor II
Hi,
Corrected code with comments below:
import arcpy # don't import modules if you're not gonna use them sys, string, os  # Get the input from the model InputTable = arcpy.GetParameterAsText(0) InputArea = arcpy.GetParameterAsText(1) InputDepth = arcpy.GetParameterAsText(2) Newfield = "Volume"  #Add Field arcpy.AddField_management(InputTable, Newfield, "DOUBLE", "", "","", "Vol","NON_NULLABLE","NON_REQUIRED","")  #Calculate Field # field names should be wrapped in exclamation marks, function uses string  #representation of expression, not real python expression Expression = '!%s! * !%s!' % (InputArea.upper(), InputDepth.upper())  #  'PYTHON' not 'Python' arcpy.CalculateField_management(InputTable, Newfield, Expression, "PYTHON")


Generally speaking code should work, although I'm not sure if txt table can be changed that way, try with .dbf.

Regards,
Arek

View solution in original post

0 Kudos
3 Replies
ArkadiuszMatoszka
Occasional Contributor II
Hi,
Corrected code with comments below:
import arcpy # don't import modules if you're not gonna use them sys, string, os  # Get the input from the model InputTable = arcpy.GetParameterAsText(0) InputArea = arcpy.GetParameterAsText(1) InputDepth = arcpy.GetParameterAsText(2) Newfield = "Volume"  #Add Field arcpy.AddField_management(InputTable, Newfield, "DOUBLE", "", "","", "Vol","NON_NULLABLE","NON_REQUIRED","")  #Calculate Field # field names should be wrapped in exclamation marks, function uses string  #representation of expression, not real python expression Expression = '!%s! * !%s!' % (InputArea.upper(), InputDepth.upper())  #  'PYTHON' not 'Python' arcpy.CalculateField_management(InputTable, Newfield, Expression, "PYTHON")


Generally speaking code should work, although I'm not sure if txt table can be changed that way, try with .dbf.

Regards,
Arek
0 Kudos
ChristianJoyner
New Contributor
Thanks for your help. I tried the new code and it still didn't work. What did you mean by  you don't think txt table can be changed that way, try with .dbf If I wrote the script right. It should be pulling the table for a feature layer and creating a new field called "Volume" which will be classed a double so numerical value. Then calculating the value for that field (volume/inputvolume) by multiplying an existing field (SArea/inputeArea) by a user imputed value (inputdepth). The error I get in python scripter is the same as before error 000732: Input Table: Dataset test.txt does not exist or is not supported. In arcmaps when I try to run the tool I get something about line 2997 in addField. So I'm not sure that my parameters and arguments maybe right for if it has to do with my script part arcpy.AddField_management(InputTable, Newfield, "DOUBLE", "", "","", "Vol","NON_NULLABLE","NON_REQUIRED","") because it's not adding the field Please help if I can understand what is going on here I will be able to get the rest of my project working well too.
0 Kudos
ArkadiuszMatoszka
Occasional Contributor II
Hi,
Your script will work fine, as long as you wont use .txt file as input. The case is that tables in txt format are read-only. If you open this test.txt table in arcmap "Add Field" option in Table options will be inactive, why then you think it can be done by arcpy.AddField?
I see two workarounds of this problem:
1). You can convert table to .dbf in script (arcpy.TableToTable_conversion) and then add and calculate field - if you don't need to keep data in txt format.
2). You can write function that open text file (open() function in python) and modifies it the way you want - it a bit harder task.

Regards.
Arek
0 Kudos