我目前正在ArcGIS中的一个项目中工作,其中有一组要素类都包含一个日期字段,该字段表示要素类中采样的日期。在
我需要将这些特性类中的每个日期(字段类型是“date”而不是字符串或整数)转换为一年中的某一天,然后将一年中的某一天添加到一个名为DOY
的空字段中。在
日期采用mm/dd/yyyy
格式,但没有前导零(7/4/15,而不是07/04/15)。每个要素类中的示例日期字段分别命名为Date
、Sample_Dat
或{
注意:在这3种情况下,arcpy.calculateField_management
应该与上面的rows.updateRows
对齐,但是当我把代码复制到这个网站上时,存在格式问题,我无法修复。在
我什么都不想编辑这一天的代码。另外,它不会给出一个红色的X或绿色的复选框,而是会给出一个黄色的三角形来发出警告,这是因为大约80%的要素类已经有了一个“DOY”字段,但我在开始时包含了addfield_管理部分,以占到没有DOY字段的20%。我认为通过添加elif语句,它不会给出警告信号,而是循环,直到到达相关语句为止。在
import arcpy
import datetime
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
if "DOY" not in arcpy.ListFields(fc):
arcpy.AddField_management(fc,"DOY","SHORT")
elif "DOY" in arcpy.ListFields(fc) and "Date" in arcpy.ListFields(fc):
with arcpy.da.UpdateCursor(fc, "Date") as rows:
for row in rows:
rows.updateRow([datetime.datetime.strptime(row, '%m/%d/%Y')])
arcpy.CalculateField_management(fc,"DOY",row.timetuple().tm_yday)
elif "DOY" in arcpy.ListFields(fc) and "Sample_Dat" in arcpy.ListFields(fc):
with arcpy.da.UpdateCursor(fc,"Sample_Dat") as rows:
for row in rows :
rows.updateRow([datetime.datetime.strptime(row, '%m/%d/%Y')])
arcpy.CalculateField_management(fc,"DOY",row.timetuple().tm_yday)
elif "DOY" in arcpy.ListFields(fc) and "T0_Date" in arcpy.ListFields(fc):
with arcpy.da.UpdateCursor(fc,"T0_Date") as rows:
for row in rows:
rows.updateRow([datetime.datetime.strptime(row, '%m/%d,%Y')])
arcpy.CalculateField_management(fc,"DOY",row.timetuple().tm_yday)
else:
pass
根据你的评论,我认为问题是你在检查DOY是否存在于每一个案例中,而不是在需要的地方创建它并在每个案例中更新它。尝试以下操作:
编辑:我检查了文档,看起来像arcpy.ListFields()返回字段对象,而不是字符串。我已经更新了我的代码。在
相关问题 更多 >
编程相关推荐