使用CalculateField 更新带有日期的字段 [英] Using CalculateField to update field with date

查看:32
本文介绍了使用CalculateField 更新带有日期的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ArcGIS 中使用 Arcpy 并尝试编写代码来更新上个月最后一天的字段(即,如果我今天在 2017-09-01 运行代码,它将更新字段与 2017-08-31).

I'm working with Arcpy in ArcGIS and trying to write code that will update a field with the last day of the previous month (ie. if I run the code today, 2017-09-01, it will update the field with 2017-08-31).

如果我进行手动字段计算,我可以使用 VBScript 和预逻辑脚本代码使其工作:

If I do a manual field calculation, I can get it to work using VBScript and the pre-logic script code:

Dim FC
FC = DateSerial(Year(Date), Month(Date), 0)

然后将该字段设置为等于 FC:

and then setting the field to equal FC:

我已尝试将其集成到代码中,以便将其设置为自动运行,但没有任何运气.有没有人能用下面的代码找出我哪里出错了?

I've tried to integrate this into code so it can be set to run automatically, but haven't had any luck. Is anyone able to identify where I'm going wrong with the below code?

import arcpy

inTable = r'C:\1 Block Watch Cr\Base_Data.gdb\Districts'
field = "Final_Date"
exp = 'getdate()'
codeblock = '''def getdate():
    DateSerial(Year(Date ()), Month(Date ()), 0)'''

arcpy.CalculateField_management(inTable, field, exp, "VB", codeblock)

我收到以下错误消息:

推荐答案

您收到错误调用子程序时无法使用括号".这是因为 VBScript 中函数调用的以下规则:

You are getting the error "Cannot use parenthesis while calling the sub". This is because of the following rules of a function call in VBScript:

在VBScript中调用函数的3种方法:

  • fn a,b - 使用这种方式时,不能将参数列表括在括号中
  • Call fn(a,b) - 当你明确写出 call 关键字时,你必须将参数列表括在括号中
  • c=fn(a,b) - 将函数返回的值赋给变量时,必须将参数列表括在括号中.
  • fn a,b - when using this way, you CANNOT enclose the parameter-list in the parenthesis
  • Call fn(a,b) - when you write the call keyword explicitly, you must enclose the parameter-list in parenthesis
  • c=fn(a,b) - when you assign the value returned by the function to a variable, you must enclose the parameter-list in the Parenthesis.

要更好地了解这些规则,请查看此答案.

To understand better about these rules, check this answer.

你的情况发生了什么:

在您发布的 VBScript 代码中,您使用了第三种方法,并且通过将参数列表括在括号内来遵循规则.因此,它运行良好.

In the VBScript code that you posted, you used the 3rd method and you followed the rule by enclosing the parameter-list inside parenthesis. Hence, it worked fine.

在 Python 代码中,您只需使用 DateSerial(Year(Date ()), Month(Date ()), 0) 这是第一种方法.根据第一种方法的规则,您不能将参数列表括在括号中.由于您确实将 param-list 括在括号内,因此您违反了该规则并得到了该错误.

In the Python code, you are simply using DateSerial(Year(Date ()), Month(Date ()), 0) which is the 1st method. According to the rule of 1st method, you must not enclose the parameter-list in the parenthesis. Since you did enclose the param-list inside parenthesis, you violated that rule and got that error.

可能的解决方案:

通过删除括号来正确使用调用方法1,如下所示:

Either use the call method 1 correctly by removing the parenthesis as below:

codeblock = '''def getdate():
    DateSerial Year(Date ()), Month(Date ()), 0'''

或在实际调用函数之前显式编写call关键字:

OR explicitly write the call keyword before actually calling the function:

codeblock = '''def getdate():
    Call DateSerial(Year(Date ()), Month(Date ()), 0)'''

或者尝试将结果日期存储在一个变量中,如下所示:

OR try storing the resulting date in a variable as below:

codeblock = '''def getdate():
    varDate = DateSerial(Year(Date ()), Month(Date ()), 0)'''

请注意,我不知道python.因此,我假设您编写的任何 Python 代码都是正确的.此错误特定于 vbScript,因此写了此答案.

Please note that I do not know python. So, I am assuming whatever python code you have written is correct. This error was specific to vbScript, hence wrote this answer.

这篇关于使用CalculateField 更新带有日期的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆