Support Questions

Find answers, ask questions, and share your expertise

Error in datetime module in python script for ni-fi

avatar
Contributor

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!

1 ACCEPTED SOLUTION

avatar
Super Guru

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.

 

 

 

 

 

 

 

 

View solution in original post

8 REPLIES 8

avatar
Super Guru

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.

 

 

 

 

 

 

 

 

avatar
Contributor

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?

 

avatar
Super Guru

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

avatar
Contributor

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')

 

avatar
Super Guru

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')

avatar
Contributor

Hello! Still same error unfortunately 😞

avatar
Super Guru

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

avatar
Contributor

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