0

Could I please get help with converting my date and time, dataː 01/01/2012 01:00 into something like 2012-01-01 01:00:00ʔ I have tried doing

datetime = [datetime.datetime(2012, 1, 1, 1, 0, 0) + datetime.timedelta(hours=i) for 
i in range(9)]

dates=[datetime.strptime('%Y-%m-%d %H:%M:%S') for x in datetime]

This returns the errorː

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

My datetime's in my csv file look like thisː

01/01/2012 01:00
01/01/2012 02:00
01/01/2012 03:00
01/01/2012 04:00
01/01/2012 05:00
01/01/2012 06:00
01/01/2012 07:00
01/01/2012 08:00
01/01/2012 09:00

I believe this must be very straight forward but i can't seem to get my head around this. help with this will be appreciated

Khan11
  • 285
  • 1
  • 3
  • 12
  • Possible duplicate of [How do I turn a python datetime into a string, with readable format date?](https://stackoverflow.com/questions/2158347/how-do-i-turn-a-python-datetime-into-a-string-with-readable-format-date) – lurker Aug 05 '18 at 19:44
  • 3
    Don't name your variable as module name `datetime`. – mvp Aug 05 '18 at 19:45
  • @mvp Thank you for replying. I have changed my variables names as so: D = [datetime.datetime(2012, 1, 1, 1, 0, 0) + D.timedelta(hours=i) for i in range(16800)] dates=[datetime.strptime('%Y-%m-%d %H:%M:%S') for x in D] but I am getting the error ot "AttributeError: type object 'datetime.datetime' has no attribute 'datetime" Is there anything else that could need changing? – Khan11 Aug 05 '18 at 20:29

1 Answers1

5

You have named your list datetime which overrides the datetime module. So datetime.strptime call fails.

Give a different name to your list.

Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • Thank you for replying. I have changed my variables names as so: D = [datetime.datetime(2012, 1, 1, 1, 0, 0) + D.timedelta(hours=i) for i in range(16800)] dates=[datetime.strptime('%Y-%m-%d %H:%M:%S') for x in D] but I am getting the error ot "AttributeError: type object 'datetime.datetime' has no attribute 'datetime" Is there anything else that needs changing? – Khan11 Aug 05 '18 at 20:05
  • Looks like you have imported the `datetime` submodule from the `datetime` module by doing `from datetime import datetime`. Instead, just import the top level module `datetime` by doing `import datetime` – Sunitha Aug 05 '18 at 20:38
  • When I import the top level module I get the original error of "AttributeError: 'list' object has no attribute 'strptime'" in the second line with the variable named dates, could there be a different issue? – Khan11 Aug 05 '18 at 20:55
  • If you have renamed your variable, there is no way you can get the origin error. Check again if you have renamed all occurrences – Sunitha Aug 05 '18 at 21:04