11
I know it’s a very simple question, but I’ve done a lot of research and I’m not finding a simple solution.
I need to create a function that receives a date in the format "28122016"
and convert her to "2016-12-28"
. How can I do that?
11
I know it’s a very simple question, but I’ve done a lot of research and I’m not finding a simple solution.
I need to create a function that receives a date in the format "28122016"
and convert her to "2016-12-28"
. How can I do that?
19
The best solution is to use the ready-made package functions datetime
. Example:
from datetime import datetime
d = datetime.strptime('28122016', '%d%m%Y')
print(d.strftime('%Y-%m-%d'))
See working in Ideone.
But, if you prefer (and if it’s a quick and punctual thing), you can also do it manually:
data = '28122016'
dia = int(data[:2])
mes = int(data[2:4])
ano = int(data[4:])
print('{:4d}-{:2d}-{:2d}'.format(ano, mes, dia))
See working in Ideone.
9
If you are sure that the format to be treated will always be ddmmyyyy
, you can create a function as follows:
my_date = lambda d: "{}-{}-{}".format(d[4:8], d[2:4], d[0:2])
To use it, simply invoke it as follows: my_date("28122016")
. The result should be 2016-12-28
.
You can see the code working here, just press the button
Run
to run the script.
The solution makes use of the method format
present in type objects string
natively. Through {}
the new text format is defined, replacing each {}
at the value passed to the method.
In this case, the first {}
shall be replaced by the value of d[4:8]
, in which d
is the value passed by the parameter when calling the function. As the string can be seen as a character array, when using d[4:8]
, we are accessing the characters in positions from 4 to 8, exclusive, ie 4, 5, 6 and 7, which will represent the year of the date. Already the second {}
shall be replaced by the value of d[2:4]
, which will represent the month, and the third {}
at the value of d[0:2]
, that will represent the day.
If you need more complex operations than this, you can use the named parameter version of the method format
, as a subsequent:
my_date = lambda d: "{year}-{month}-{day}".format(day = d[0:2], month = d[2:4], year = d[4:8])
The result will be exactly the same, but with the most human-readable format.
For more information on the method
format
, click here. Official documentation on strings: here.
Despite being basically the same solution already proposed, takes my +1 by the effort to explain step by step and by indicating the named version of parameters in format
. :)
Yeah, I didn’t see that they answered while I was formulating the kkk answer
Relax. You can never have too much knowledge and your answer is really good. :)
I did it this way that you explained and had the return of leaving so <Function Regc100.init.<locals>.convData.<locals>. <lambda> at 0x0000014175576EA0>
@Ricardogomes, this is because you did not invoke my_date as a function. Try to run my_date("20161228").
that was the problem, I was putting it inside a function and this function doing my_date did not understand very well why my_date became a function more worked
@Ricardogomes, my_date is a function because I define it using the lambda instruction, which in python is used to create anonymous functions. In this case, an anonymous function is created and assigned to the my_date object (remembering that in python everything is object), so it is necessary to invoke my_date as a function. You could define my_date as a function normally, through the def instruction, that the result would be exactly the same. It is only a matter of using lambda for simple functions, from only one command line.
Browser other questions tagged python python-3.x
You are not signed in. Login or sign up in order to post.
Excellent Luiz, I didn’t know
– Miguel
A question Luiz, in his second example, why
'{:4d}-{:2d}-{:2d}'
? Why not'{}-{}-{}'
in this case?– Miguel
@Miguel Just to make it clear that you can format the whole with the size ("padding") desired. Same reason why I did the casting for
int
previously: my stubborn didactic. rs Neither needed.– Luiz Vieira
Exactly, that’s what I thought, but it could be because of another reason I didn’t know. Obagdo
– Miguel