Python Forum
Pass integers to datetime.date function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass integers to datetime.date function
#1
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.
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 subscriptable
Which 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? Wall
Reply
#2
I think one of the problems might be that you are passing strings to datetime.date, so you should first convert it to int, but that's probably not the cause of 'int' object is not subscriptable Think
Reply
#3
I can't run your datetime code independently, since I don't have your data.

Can you put in a static dictionary into that code that reproduces your problem (for folks that don't have your exact xml)?
Reply
#4
I would note the following: 1) OP tries to parse xml file and assume that if <wp:post_date> is in x, then x should contain some data. x is a string and might include just <wp:post_date> tag. I would recommend to use xml-parser instead; (the same is true for title tag) 2) It is recommended to open files using with keyword.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 212 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 585 Jan-20-2024, 04:45 AM
Last Post: 1418
  How to pass encrypted pass to pyodbc script tester_V 0 851 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,984 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Filter dataframe by datetime.date column glidecode 2 5,104 Dec-05-2021, 12:51 AM
Last Post: glidecode
  Date format and past date check function Turtle 5 4,228 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,576 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Possible to dynamically pass arguments to a function? grimm1111 2 2,169 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,223 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Convert date integers (ex. 43831) to regular format Galven 2 2,605 Nov-15-2020, 11:38 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020