Skip to content Skip to sidebar Skip to footer

Getting The Enclosed Content

I want to get the enclosed content. Split is only allowing me to split by one delimiter. Input: \x02\xxx\xxx\xxx\xxx\x03\x02\xxx\xxx\xxx\xxx\x03\x02\xxx\xxx\xxx\xxx\x03 Wanted Out

Solution 1:

I suspect you'll need to say more about the possible variations in input, but this code would work for the specific case you mention:

line = '\x02\xxx\xxx\xxx\x03\x02\xxx\xxx\xxx\x03\x02\xxx\xxx\xxx\x03'if line.startswith('\x02'):
    line = line[4:]
if line.endswith('\x03'):
    line = line[:-4]
chunks = line.split('\x03\x02')

print chunks

>>> ('\xxx\xxx\xxx', '\xxx\xxx\xxx', '\xxx\xxx\xxx')

If every line started with '\x02' and ended with '\x03', this simpler approach would work:

line = line[4:-4]
chunks = line.split('\x03\x02')

If there's much variation in the delimiters, I like root's suggestion to look at the re.split() method: http://docs.python.org/2/library/re.html#re.split.

Solution 2:

You can use re.split() from the re module that allows you to split on a pattern. Something like filter(None,re.split(r'\\x0\d',s)) should work.

Post a Comment for "Getting The Enclosed Content"