使用 Python 在 ArcGIS 中添加多个字段名称

发布于 2024-09-27 02:01:15 字数 810 浏览 1 评论 0原文

我想知道是否有人会帮助我完成我的硬件任务......

到目前为止,这是我的 python 程序 - 它做了它应该做的事情。

# 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", "")

我的任务是解压第二个参数 (sys.argv[2]),将其分解为列表中的多个项目,然后将添加字段代码包含在循环中以添加多个字段值。

显然,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 much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

望笑 2024-10-04 02:01:15

要将 sys.argv[2] 分解为由任意字符分隔的列表,请使用以下格式:sys.argv[2].split(<要分割的字符>) 其中“要分割的字符”如果您想用正斜杠字符分隔,by" 可以是类似“/”的内容;如果您想用逗号分隔,则可以是“,”之类的内容。在 Python 的交互模式下尝试,并在将为 sys.argv[2] 输入的某些字符串上进行测试。要循环访问列表,请使用: for i in range(len()): 来迭代我向您展示的 split 函数返回的列表中的所有项目。我不想放弃一切,所以我会把你留在那里,希望你能弄清楚剩下的事情!祝你好运。

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.

萌辣 2024-10-04 02:01:15

我使用模型生成器创建了一个基本脚本来将字段添加到表中。我的任务是 (1) 能够向 Arc 中的数据表添加六个字段。 (2) 将数据集的结果写入此表。

通过我所做的工作,我可以将多个字段添加到我在 Arc 中创建的空 shapefile 中,但我必须每次运行脚本并手动输入字段名称。 (这一切都是在 Arc 中运行的,但我正在 IDLE 中编辑它)。

上面的脚本是我最初的脚本。我添加了新字段的列表:

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

我将分割添加到了 sys.argv[2] 的末尾,

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

这是我收到错误消息的地方

我将脚本的最后一行放入循环中

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

所以,我需要“解压sys.argv[2] 到列表中的多个项目中,然后将添加字段代码包含在循环中以添加多个字段值。”

我是否需要考虑标题中的范围从 3 到 8。

我花了几个小时阅读(并试图理解)我能找到的所有在线 python 帮助。我真的很感谢你的所有帮助。

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.

梦与时光遇 2024-10-04 02:01:15

首先,您收到的错误消息到底是什么?你给你的脚本提供了什么输入(我想看看你如何从命令行运行python脚本,比如:'python myFile.py arg1 arg2')?

您将需要这样的内容:

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

如果这解决了您的问题,请告诉我。

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.

浅紫色的梦幻 2024-10-04 02:01:15

1)您可以使用以下命令跳过新数组的迭代器:
for item in new_fields:

这会将新变量 item 设置为数组每个部分的值。

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

2)你的错误是因为你正在分割一个数组对象。 Split 旨在处理字符串对象。如果你有:

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

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

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"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文