Home > other >  Python for help. The following code encapsulation to function
Python for help. The following code encapsulation to function

Time:09-26

Import refrom datetime import datetime, timedeltainfos=[{" id ":" 001 ", "time" : "5 hours before"}, {" id ":" 002 ", "time", "10 minutes before"}, {" id ":" 004 ", "time", "3 days ago"}, {" id ":" 008 ", "time" : "just"},] # encapsulate the following code to the function for item in infos: time_now=datetime. Now () # item [' time '] time_str=item [' time '] rn=re. The.findall (r '\ d {1, 2}, time_str) if len (rn) & gt; 0: n=int (rn) [0]) else: n=0 if 'hours' time_str: in time=time_now + timedelta (hours=- n) elif' minutes' in time_str: time=time_now + timedelta (minutes=- n) elif 'day in time_str: time=time_now + timedelta (days=- n) else: time=time_now item [' time']=time. The strftime (' % % Y - m - H: % d % % m: % S ') print (infos)




Class a bit do not understand it please, through passing bosses help me have a look at how to write the

CodePudding user response:

 

The import re
The from datetime import datetime, timedelta

Infos=[{" id ":" 001 ", "time" : "5 hours before"},
{" id ":" 002 ", "time", "10 minutes before"},
{" id ":" 004 ", "time", "3 days ago"},
{" id ":" 008 ", "time" : "just"},]
# encapsulate the code below into the function in the
Def work (infos) :
For the item in infos:
# the rest code copied... , pay attention to the indentation

The work (infos)





That is what you mean,

CodePudding user response:

reference 1/f, old coconut response:
 

The import re
The from datetime import datetime, timedelta

Infos=[{" id ":" 001 ", "time" : "5 hours before"},
{" id ":" 002 ", "time", "10 minutes before"},
{" id ":" 004 ", "time", "3 days ago"},
{" id ":" 008 ", "time" : "just"},]
# encapsulate the code below into the function in the
Def work (infos) :
For the item in infos:
# the rest code copied... , pay attention to the indentation

The work (infos)





That is what you mean,

Thank the bosses you write simple a lot, I groped for the whole yesterday, didn't you this is simple
  • Related