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

查看:173
本文介绍了使用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:

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

  • 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代码中,您使用了第3种方法,并遵循了该规则,将参数列表括在括号内.因此,它工作正常.

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天全站免登陆