tags:

views:

85

answers:

3

I was wondering if anyone out there would help me with a step in my HW assignment...

So far this is my python program - which does what it is supposed to.

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Script argum ents...
Input_Table = sys.argv[1]
gp.addmessage("sys.argv[1] is " + Input_Table)

Field_Name = sys.argv[2]
gp.addmessage("sys.argv[2] is " + Field_Name)

# Local variables...
Output_Feature_Class = ""

# Process: Add Field...
gp.AddField_management(Input_Table,     Field_Name, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

My task is to unpack my second parameter (sys.argv[2]) to break it into multiple items in a list and then enlose the add filed code in a loop to add the multiple field values.

Obviously, python is not my thing. I am sure this is an easy task.

Thanks so much!

A: 

To break up sys.argv[2] into a list separated by any character, use this format: sys.argv[2].split(<character to split by>) where "character to split by" can be something like "/" if you want to split by the forward slash character or "," if you want to split by a comma. Try it in Python's interactive mode and test it on some string that will be entered for sys.argv[2]. To loop through the list use a: for i in range(len(<list name>)): to iterate through all items in the list returned by the split function I showed you. I don't want to give away everything, so I will leave you there and hopefully you can figure out the rest! Good luck.

Stunner
Thanks for the help, but I am still lost.
Harper_C
I've created a list of all the values I wish to add new_fields=['DOM_CLASS', 'DOM_LIMIT', 'DOM_VAL', 'SUB_CLASS', 'SUB_LIMIT', 'SUB_VAL'] When I add split I get the error message <type 'exceptions.TypeError'>: cannot concatenate 'str' and 'list' objectsFailed to execute (addFields). I put the loop aroune the last string: for i in range (len(new_fields)): gp.AddField_management(Input_Table, Field_Name, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
Harper_C
I am testing this on an empty point shapefile. If you have anymore advice, I'd really appreciate it. Thanks!
Harper_C
You can't call split on a list. You can call split on an item within a list, like so: lst = ['dog_cat","cat_dog"]. Calling split: lst[0].split("_") = ["dog","cat"]. When iterating through the for loop you will access all elements of the list using the following format: for i in range(len(new_fields)): new_fields[i]//you have grabbed some string in new_fields, i starts at 0 and iterates upward until len(new_fields)-1. I am still not quite sure what it is you want to do, if you fill me in maybe I can help you more. Don't forget about googling or trying out some Python tutorials!
Stunner
I tried to explain my question better in an answer to my question. It seemed the easiest and neatest way to explain. Thanks
Harper_C
A: 

Using model builder I created a basic script to add fields to a table. My task is to (1) be able to add six fields to a data table in Arc. (2) Write results from a dataset to this table.

With what I have done I am able to add multiple fields to an empty shapefile I created in Arc but I have to run the script each time and manually input the field name. (This is all run in Arc, but I am editing it in IDLE).

The above script is what I initially had. I added a list of the new fields:

new_fields=['DOM_CLASS', 'DOM_LIMIT', 'DOM_VAL', 'SUB_CLASS', 'SUB_LIMIT', 'SUB_VAL']

I added the split to the end of my sys.argv[2]

 Field_Name = sys.argv[2].split(',')

this is whereI get the error message

I put the last line of the script in a loop

 for i in range (len(new_fields)):
     gp.AddField_management(Input_Table, Field_Name, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

So, I need to "unpack the sys.argv[2] into multiple items in a list and then enclose the add field code in a loop to add the multiple field values."

Do I need to take in account the range in the headers will be from 3 to 8.

I have spent hours reading (and trying to understand) all the online python help I can find. I really appreciate all your help.

Harper_C
A: 

First off, what error message is it that you get, exactly? And what input are you giving your script(I want to see how you run the python script from command line, something like: 'python myFile.py arg1 arg2')?

You will need something like this:

for i in range(len(new_fields)): 
     Field_Name = new_fields[i] 
     gp.AddField_management(Input_Table, Field_Name, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

Let me know if this solved your issue.

Stunner
My code is exactly what I have written above. I am only editing in Python. I ma running it as a script in Arc. My script properties are:
Harper_C
Layer: Table View, New Field Names: Any Value, Debug: Boolean . I only get an error message when I try to put the split thing you mentioned.
Harper_C
I strongly URGE you read some python tutorials or guides as it will be much harder learning it by me simply telling you the answers and waiting a couple of days each time for me to get back to you. It is very apparent that you do not have a good grasp on Python and taking the initiative to teach yourself will only help you in the long run and make your life much MUCH easier. Anyways, look up to this answer to see the edits I have made. The code I pasted should take care of the issue you are having. To understand why, you will have to consult a tutorial yourself(google it!).
Stunner