Jul-17-2020, 05:43 PM
Hello,
I'm relatively new to the Python world. I'm trying to write a script that checks if a post has been published within the last seven days.
For this I store the title of the post as the key and the publishing date as the value in a dictionary.
I'm relatively new to the Python world. I'm trying to write a script that checks if a post has been published within the last seven days.
For this I store the title of the post as the key and the publishing date as the value in a dictionary.
fhandle = open("filename.xml", "r") title_dict = dict() import datetime for x in fhandle: if "<title>" in x: y = x.strip() z = y[7:-8] title_dict[z] = title_dict.get(z,0) + 1 elif "<wp:post_date>" in x: cleanedup = x.strip() titledate = cleanedup[23:-27] year = titledate[:4] month = titledate[5:-3] month = month.lstrip("0") day = titledate[8:] day = day.lstrip("0") titledate = list() titledate.append(year) titledate.append(month) titledate.append(day) title_dict_temp = {z: (year, month, day)} title_dict.update(title_dict_temp print(title_dict)Leads to:
{'Title of Post': ('2020', '7', '16'), 'Title of Post2': ('2020', '7', '16')}Then the datetime module:
import datetime today = datetime.date.today() margin = datetime.timedelta(days = 7) for k,v in title_dict.items(): if today - margin <= datetime.date(v[0], v[1], v[2]) <= today + margin: print("Within date range")I get the following error:
'int' object is not subscriptableWhich makes no sense to me, isn't this a tuple from which I'm trying to extract the integers in order to pass as arguments to the date-method?
for k,v in title_dict.items(): if today - margin <= datetime.date(v[0], v[1], v[2]) <= today + margin: print("Within date range")
<class 'tuple'>On the other hand, when I try sanity testing my process, it works just fine:
test_dict = {'Title of Post': ('2020', '7', '16'), 'Title of Post2': ('2020', '7', '16')} for k,v in test_dict.items(): print(v[1])What am I overlooking here?
