Skip to main content
Brenigan
Gold ⭐️
June 3, 2022
Solved

Error in datetime module in python script for ni-fi

So I am trying to implement this script

 

 

from datetime import datetime, timedelta, date                                         

flowfile = session.get()
current_date= datetime.today().date()
if flowfile != None:
src_table_name = flowfile.getAttribute('src_table_name')
date_filter = str(current_date.replace(day =11))
if src_table_name in ['Income_project_BP', 'Plan_final_scenario', 'Plan_primary_scenario', 'ProductionCalendar', 'Subcontracting_BP']:
flowfile = session.putAttribute(flowfile, 'date_filter', date_filter)
session.transfer(flowfile, REL_SUCCESS)
else:
session.transfer(flowfile, REL_FAILURE)

 

 

 

And I've tried a lot of different ways but when it comes to "replace" method I'm always getting this error. Same thing when I am trying to use "strftime" method

11111.jpg

 

What should I do? I really need this method.


Thank you beforehand!

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
June 3, 2022

Hi,

I think something is happening when nifi is trying to interpret the correct data type in python. to make it work make the current_date string as follows:

current_date= str(datetime.today().date())

and let the python engine do the conversion to the correct datetime type when you set the date_filter as follows:

date_filter = str(datetime.strptime(current_date,'%Y-%m-%d').replace(day =11))

 

Not sure if this is a bug in Nifi, but at least this should be a work around.

Hope that helps.

 

 

 

 

 

 

 

 

8 replies

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
June 3, 2022

Hi,

I think something is happening when nifi is trying to interpret the correct data type in python. to make it work make the current_date string as follows:

current_date= str(datetime.today().date())

and let the python engine do the conversion to the correct datetime type when you set the date_filter as follows:

date_filter = str(datetime.strptime(current_date,'%Y-%m-%d').replace(day =11))

 

Not sure if this is a bug in Nifi, but at least this should be a work around.

Hope that helps.

 

 

 

 

 

 

 

 

Translate
Brenigan
BreniganAuthor
Gold ⭐️
June 3, 2022

Hello! Thank you so much it worked!
Can you please explain me how can I make date_filter with format %Y%m%d instead of %Y-%m-%d?

 

Translate
SAMSAL
Platinum ⭐️⭐️⭐️
June 4, 2022

set the date filter as follows:

date_filter = datetime.strptime(current_date,'%Y-%m-%d').replace(day =11).strftime('%Y%m%d')

 

If that solves your problem please accept solution.

Thanks

Translate
Brenigan
BreniganAuthor
Gold ⭐️
June 4, 2022

thank you again. kind sir!!
I am trying to do this to increase a year, but ni-fi says there are no viable alternative at input '='
what am I doing wrong?

 

 

end_next_year =  datetime.strptime(current_date,'%Y-%m-%d').replace(year = datetime.strptime(current_date,'%Y-%m-%d').year + 1).strftime('%Y1231')

 

Translate
SAMSAL
Platinum ⭐️⭐️⭐️
June 4, 2022

Try this:

end_next_year = datetime.strptime(current_date,'%Y-%m-%d').replace(year = datetime.strptime(current_date,'%Y-%m-%d').year+1).strftime('%Y%m%d')

Translate
Brenigan
BreniganAuthor
Gold ⭐️
June 4, 2022

Hello! Still same error unfortunately 😞

Translate
SAMSAL
Platinum ⭐️⭐️⭐️
June 4, 2022

It worked for me. I have tried it before posting it and got the expected result. make sure the old code that was throwing the error is not there

Translate
Brenigan
BreniganAuthor
Gold ⭐️
June 5, 2022

I've installed the last version of ni-fi and it solved all problems! 
Thank you!

Translate