如何将列字段转换为列表
我正在尝试使用 ArcGIS 9.3 进行简单的字段计算:
New field = Old field - Old field(first value)
在使用 python 代码计算字段中应该是
表达式:!旧字段! -first
代码块:list= [AngOriz]first = list[0]
错误是
ERROR 000539: Runtime error : name 'AngOriz' is not defined Failed to execute (Calculate Field).
如何将列字段转换为列表?
我已经尝试过这种方式
表达式:
makeCalc( !AngOriz!, !AngOriz!)
代码块:
def makeCalc(x, y):
first_value = y.split(' , ')[0]
return x-first_value
但我仍然得到:
错误 000539:运行表达式时出错:makeCalc( 43.01841, 43.01841) :“float”对象没有属性“split”,无法执行(计算字段)。
我需要计算列的值与同一列的第一个值之间的差(值是浮点)。 目的是计算滑坡上不同测量点的位移。
I'm trying to make a simple field calculation with ArcGIS 9.3:
New field = Old field - Old field(first value)
which in Calculate Field with python code should be
Expression: !Old field! - first
Code Block: list= [AngOriz] first = list[0]
The error is
ERROR 000539: Runtime error : name 'AngOriz' is not defined Failed to execute (Calculate Field).
How could I tranform a column field into a list?
I've tried this way
Expression:
makeCalc( !AngOriz!, !AngOriz!)
Code Block:
def makeCalc(x, y):
first_value = y.split(' , ')[0]
return x-first_value
but still I get:
ERROR 000539: Error running expression: makeCalc( 43.01841, 43.01841) : 'float' object has no attribute 'split' Failed to execute (Calculate Field).
I need to calculate the difference between a value of a column and the first value of the same column (values are floating points).
The purpose is to calculate the displacement of different survey points on a landslide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白整个情况,旧字段(第一个值)是什么意思?旧字段是一个文本字段,其中包含多个值,并用逗号或类似的内容分隔?如果是这样,那么您之前为什么使用 Old 字段?
但无论如何,你得到的错误是因为在代码块上你没有定义 AngOriz...做你想做的事情的最好方法是在一个函数内部,它获取必要的值来使用参数,并将值返回到字段,这里是一个示例:
在表达式字段上:
和在代码块上:
I'm not getting the whole picture, what do you mean by Old field(first value)?? is Old field a text field with multiple values on it separated by commas or something like that? and if so, how come you use Old field before that?
But anyway, the error you're getting is because on the code block you haven't defined AngOriz... the best way to do what you're trying to do is inside a function, which gets the necessary values to work with as parameters, and returns the value to the field, here is an example:
on the Expression field:
and on the codeblock:
抱歉这么久才回答。度过了忙碌的一周:-) 好吧,我以为你的意思是字符串字段,而不是浮点字段,所以忘记拆分的东西。我将答案留在上面,因为它可能会帮助其他人寻找如何将(字符串)字段转换为列表并使用其值进行字段计算。
如果我现在明白了,您的意思是 AngOriz 列第一行的值,对吧?因此,如果这个值没有改变,那么只需在函数上使用它“硬编码”,如下所示:
在表达式字段上:
和在代码块上:
如果您不想“硬编码”第一行的值,那么您将不得不以某种方式检索它,但我不确定如何在现场计算器上执行此操作。也许您必须以完全不同的方式解决这个问题。使用 arcpy 左右的脚本。
更新:
还可以在字段计算器内使用全局变量 - 这意味着变量在函数调用之间保持不变。因为该函数由字段计算器为每一行调用,并且通常函数内的所有变量在调用之间“死亡”,所以不可能“记住”局部变量上的某些内容。但是您可以使用全局变量来保存第一行的值并在其他后续调用中使用它。我还没有尝试过,但也许这样的事情会起作用:
Sorry for taking so long to answer. Had a busy week :-) Ok, I thought you meant a string field, not a float field, so forget about the split-stuff. I'm leaving the answer above, as it may help someone else looking for how to transform a (string) field into a list and use its values for a field calculation.
If I'm getting this right now, you mean the value of the first row of column AngOriz, right? So if this value doesn't change, then just use it "hard coded" on the function like this:
on the Expression field:
and on the codeblock:
If you don't want to "hard code" the value of the first row, then you would have to retrieve it somehow, but I'm not sure how you could do this on the field calculator. Probably you would have to solve this in a completely different way. With a script in arcpy or so.
Update:
There is also the possibility of using global variables inside the field calculator - this means variables that are persistent between function calls. Because the function gets called by the field calculator for each row, and normally all variables inside a function "die" between calls, it is impossible to "remember" something on local variables. But you could use a global variable to save the first row's value and use it on the other following calls. I haven't tried it, but maybe something like this would work: