添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a Date field in an ArcGIS file geodatabase feature class called "DateTemp1." I created a new text field called "DateTemp2" and want to calculate that based on the DateTemp1 field, but I am getting a unicode error.

TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'unicode'

 arcpy.CalculateField_management("all_reports.gdb/lsr_reports1", "DateTemp2", "datetime.datetime.strftime(!DateTemp1!, '%Y%m%d')", "PYTHON_9.3", "")
                Your chosen solution works well if you need to control the exact format, but keep in mind that the error you received originally is caused by the fact that CalculateField passes the date field as a string into Python. In other words, you could have simply used the expression !DateTemp1! and you would have gotten a string formatted using the default expression (usually MM/DD/YYYY).
– Evil Genius
                May 4, 2016 at 12:33
                You're right I tried that as well and it did work. The string was formatted MM/DD/YYYY as you stated. I do want the final string to be in the format YYYYMMDD. Using Convert Time Field it keeps the hours and minutes (YYYYMMDDHHMM). Now I am just trying to figure out how to dump the HHMM and create a new field, but that is another question. Thanks.
– Andrew
                May 4, 2016 at 15:15

I looked into using the Convert Time Field tool as the poster suggested and it worked.

inTable  = "all_reports.gdb/lsr_reports1"
inputTimeField = "DateTemp1"
inputTimeFormat ="dd/MM/yyyy HH:mm:ss;AM;PM"
outputDateField = "DateTemp2"
arcpy.ConvertTimeField_management(inTable, inputTimeField, inputTimeFormat, outputDateField)

Try using "datetime.datetime.strptime( !DateTemp1! , '%d/%m/%Y').strftime('%Y%m%d')" as your expression:

arcpy.CalculateField_management("all_reports.gdb/lsr_reports1", "DateTemp2", "datetime.datetime.strptime( !DateTemp1! , '%d/%m/%Y').strftime('%Y%m%d')", "PYTHON_9.3", "")

NOTE you will need to change the '%d/%m/%Y' from in strptime( !DateTemp1! , '%d/%m/%Y') to match the current format of your existing date field

This works well too. I am going with this but I had to change the syntax to match the existing date field. "datetime.datetime.strptime( !DateTemp1! , '%d/%m/%Y %I:%M:%S %p').strftime('%Y%d%m')" – Andrew May 4, 2016 at 15:38

Thanks for contributing an answer to Geographic Information Systems Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.