开发者

Adding multiple field names in ArcGIS using Python

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 m开发者_JAVA百科uch!


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.


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.


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.


1)You could skip the iterator for the new array with: for item in new_fields:

Which would set the new variable item to the value of each portion of the array.

for item in new_fields:
    gp.AddField_management(Input_Table,item,"TEXT","","","","","NULLABLE","NON_REQUIRED","")

2) your error is because you're splitting an array object. Split is designed to work on a string object. If you had:

new_fields = "DOM_CLASS,DOM_LIMIT,DOM_1,DOM_2"
Split_fields = new_fields.split(',')

Split_Fields would then = ["DOM_CLASS","DOM_LIMIT","DOM_1","DOM_2"]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜