Converting string date to timestamp in python 3.4

旧时模样 提交于 2019-11-27 08:35:17

问题


I am trying to convert string date to timestamp in Python as described in the post here. When I run the code examples in the post, I encounter an error. For e.g.:

>>> import time
>>> import datetime
>>> s = "01/12/2011"
>>> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
  File "C:\Python34\lib\_strptime.py", line 15, in <module>
    import calendar
  File "C:\Python34\calendar.py", line 9, in <module>
    calendar = wckCalendar.Calendar(root, command=echo)
NameError: name 'wckCalendar' is not defined
>>> 

It looks like at runtime the code implicitly tries to import calendar and throws the error in the process. When I import calendar directly, I get the same error:

>>> import calendar
Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    import calendar
  File "C:\Python34\calendar.py", line 9, in <module>
    calendar = wckCalendar.Calendar(root, command=echo)
NameError: name 'wckCalendar' is not defined
>>> 

I just want to be able to do the following:

>>> import time
>>> import datetime
>>> s = "01/12/2011"
>>> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())

Any ideas?

Python 3.4.1 on Windows 7 32 bit*


回答1:


Thanks to @J.F. Sebastian. I was caught in the name shadowing trap. I made the stupid mistake of naming one of my modules calendar.py. Once I removed this file, everything ran fine. I hope this post is not a waste and that someone may someday find it useful.



来源:https://stackoverflow.com/questions/25299371/converting-string-date-to-timestamp-in-python-3-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!