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.
Leads to:
Then the datetime module:
I get the following error:
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?
On the other hand, when I try sanity testing my process, it works just fine:
What am I overlooking here?
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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) |
1 |
{ 'Title of Post' : ( '2020' , '7' , '16' ), 'Title of Post2' : ( '2020' , '7' , '16' )} |
1 2 3 4 5 6 |
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" ) |
1 |
'int' object is not subscriptable |
1 2 3 |
for k,v in title_dict.items(): if today - margin < = datetime.date(v[ 0 ], v[ 1 ], v[ 2 ]) < = today + margin: print ( "Within date range" ) |
1 |
< class 'tuple' > |
1 2 3 |
test_dict = { 'Title of Post' : ( '2020' , '7' , '16' ), 'Title of Post2' : ( '2020' , '7' , '16' )} for k,v in test_dict.items(): print (v[ 1 ]) |
