문제

I'm trying to split datetime... it works well to store date but I get an error whenever I try and store time.

The following code works:

datetime =  tweet.date.encode( 'ascii', 'ignore')
struct_date = time.strptime(datetime, "%a, %d %b %Y %H:%M:%S +0000")
date = time.strftime("%m/%d/%Y")

But if I add the following line, I get an error:

  time = time.strftime("%H:%M:%S")

AttributeError: 'str' object has no attribute 'strptime'

도움이 되었습니까?

해결책

You assigned a string to a variable named time. Use a different name instead, it is masking your time module import.

tm = time.strptime(datetime, "%H:%M:%S")

다른 팁

It probably worked once and then stopped working because you overwrote the module 'time' with a variable named 'time'. Use a different variable name.

This overwrites the time module

>>> import time
>>> type(time)
<type 'module'>
>>> time = time.strftime("%H:%M:%S")
>>> type(time)
<type 'str'>
>>> time = time.strftime("%H:%M:%S")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'strftime'

This is how you should do it

>>> import time
>>> type(time)
<type 'module'>
>>> mytime = time.strftime("%H:%M:%S")
>>> type(time)
<type 'module'>
>>> time.strftime("%H:%M:%S")
'11:05:08'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top