Skip to content Skip to sidebar Skip to footer

Very Specific Substring Retrieval And Split

i know there are tons of posts about sub-stringing, believe me i have searched through many of them looking for an answer to this. i have many strings, lines from a log, and i am t

Solution 1:

You can limit the split to the first ';' only:

static, dynamic = line.split(';', 1)

Your static part splitting might take a little more doing, but if you know the number of spaces is going to be static in the first part, perhaps the same trick could work there:

static = static.split(' ', 4)[-1]

If the first part of the line is more complex (spaces in the TYPE part) I fear that removing everything before that is going to be a more difficult affair. Your best bet is to figure out the limited set of values TYPE could assume and to use a regular expression with that information to split the static part.

Solution 2:

You could try something like:

>>> regexp = re.compile("^([\/.\w]*)\:(\w{3}\s\d{2}\s\d{2}\:\d{2}\:\d{2})\s([A-Z]*)\s([\w\s]*)\;([\w\s]*)$")
>>> regexp.match(line).groups()
('/long/file/name/with.dots.and.extension', 'Jan 01 12:00:00', 'TYPE', 'Static Message', 'Dynamic Message')

Post a Comment for "Very Specific Substring Retrieval And Split"