Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime strptime - set format to ignore trailing part of string

I have a string with variable length and I want to give a format to strptime in order for the rest of the string to be ignored. Let me exemplify. I have something like

9/4/2013,00:00:00,7.8,7.4,9.53
10/4/2013,00:00:00,8.64,7.4,9.53

and I want a format that makes the command strptime(line,format) work to read those lines. Something like format='%d/%m/%Y,%H:%M:%S*', although I know that doesn't work. I guess my question is kind of similar to this one, but no answer there could help me and my problem is a little worse because the full length of my string can vary. I have a feeling that dateutil could solve my problem, but I can't find something there that does the trick.

I can probably do something like strptime(''.join(line.split(',')[:2]),format), but I wouldn't want to resort to that for user-related issues.

like image 863
TomCho Avatar asked Mar 26 '15 17:03

TomCho


People also ask

What is the difference between Strptime and Strftime?

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.

What does the P stand for in Strptime?

strptime() -> string parsed time.

What does datetime Strptime do in Python?

Python time strptime() function The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it.

Is Strptime aware a timezone?

When the %z directive is provided to the strptime() method, an aware datetime object will be produced. The tzinfo of the result will be set to a timezone instance.


1 Answers

You cannot have datetime.strptime() ignore part of the input.; your only option really is to split off the extra text first.

So yes, you do have to split and rejoin your string:

format = '%d/%m/%Y,%H:%M:%S'
datetime.strptime(','.join(line.split(',', 2)[:2]), format)

or find some other means to extract the information. You could use a regular expression, for example:

datetime_pattern = re.compile(r'(\d{1,2}/\d{1,2}/\d{4},\d{2}:\d{2}:\d{2})')
format = '%d/%m/%Y,%H:%M:%S'
datetime.strptime(datetime_pattern.search(line).group(), format)
like image 85
Martijn Pieters Avatar answered Sep 20 '22 15:09

Martijn Pieters