Aug-06-2020, 02:04 PM
(This post was last modified: Aug-06-2020, 08:23 PM by Drone4four.)
I’m trying to extract the year from a date of a string. One way of doing it would be to use string slicing which is easy and obvious. But I am trying to learn how to extract a year using timedate and strptime().
For example, given a string such as ‘8 Aug, 2015’, I am trying to convert it to a tuple using the
Here is my script:
Could someone clarify what this trace back is saying?
What is a better way of extracting the year from a date given a string using strptime (instead of string slicing)?
I tried replacing line 5 with:
Resources that I have used so far:
For example, given a string such as ‘8 Aug, 2015’, I am trying to convert it to a tuple using the
strptime()
built-in by formatting it with %d %b,%Y
and then pull the year off and return it when the function is called.Here is my script:
from datetime import datetime def extract_year(date_parm): date_string_obj = datetime.strptime(date_parm, "%d %b %Y") year_obj = date_string_obj[2] return year_obj print(extract_year('8 Aug 2015'))Here is my traceback:
Error:$ python basic_dt_convert.py
Traceback (most recent call last):
File "basic_dt_convert.py", line 8, in <module>
print(extract_year('8 Aug 2015'))
File "basic_dt_convert.py", line 5, in extract_year
year_obj = date_string_obj[2]
TypeError: 'datetime.datetime' object is not subscriptable
The traceback is pointing to line 5 when I am trying to assign the second item in the newly created tuple to a variable. The ‘TypeError: 'datetime.datetime' object is not subscriptable’ means that the date_string_obj
can’t be indexed. But my understanding is that date_string_obj should be a tuple which, even though it is immutable, it is still an iterable type so with [2]
, Python should still be pulling the 3rd index position and assigning it to ‘year_obj’. I’m missing something here. Could someone clarify what this trace back is saying?
What is a better way of extracting the year from a date given a string using strptime (instead of string slicing)?
I tried replacing line 5 with:
year_obj = datetime.date_string_obj.year()
so my script now looks like this:from datetime import datetime def extract_year(date_parm): date_string_obj = datetime.strptime(date_parm, "%d %b %Y") year_obj = datetime.date_string_obj.year() return year_obj print(extract_year('8 Aug 2015'))Now Python is throwing an attribute error at line 5. How do I properly extract the year?
Resources that I have used so far:
- The “strftime() and strptime() Behavior” in the official Python docs.
- I Googled ‘strftime string to datetime year’ which turned up:
- A Programiz tutorial titled “Python strftime()”
- “Python string to datetime – strptime()” on Journal Dev
- Stack Abuse’s “Converting Strings to datetime in Python”
- A Programiz tutorial titled “Python strftime()”
- I also found a very relevant (but slightly outdated) Stack Overflow answer to a question titled: “How to extract the year from a Python datetime object?”