开发者

Using datetime.strptime to get date from string using python

i have a bunch of strings like this: s = 3851102123

objective would be to extract the date from it in example:

85 is ye开发者_如何学Goar

11 is month

02 is day

basically string indexes 1,2 = year, 3,4 = month, 5,6 = day

rest can be discarded, this is what i came up with:

>> x = datetime.strptime("3851102123", ??)

How would one extract the proper indexes?

Should i use regular expressions inside the parentheses to get the date out and then reformat the string ?

Output should like this:

>> 02.11.1985 


I am not sure if you can do it like that, as strptime will expect a format string and the format string does not allow for variable fields (as you'll need if you want the format to ignore the first number of the string)

You could do something like this: datetime.strptime(the_string[1:7], "%y%m%d").strftime("%d.%m.%Y")

but maybe substrings or regular expressions would be faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜