0

I've a string like:

10-24-2017 10:09:18.218 - my_test - INFO - My Automation version 0.0.1

And I wish to split the string by the token " - " (Note the leading and lagging white-spaces). IOW, the above string should be split as:

{'10-24-2017 10:09:18.218', 'my_test', 'INFO', 'My Automation version 0.0.1'}

If I just split by '-', then the date string will also be split which I don't wish to do. Can someone point me in the right direction?

Thanks

1
  • 3
    ... Did you try splitting by " - " yet? Commented Oct 24, 2017 at 22:40

4 Answers 4

2

You can use re.split instead:

In [3]: re.split(' - ', '123-456 - foo - bar')
Out[3]: ['123-456', 'foo', 'bar']

Or just split by the whole string:

In [5]: '123-456 - foo - bar'.split(' - ')
Out[5]: ['123-456', 'foo', 'bar']
Sign up to request clarification or add additional context in comments.

Comments

2
'10-24-2017 10:09:18.218 - my_test - INFO - My Automation version 0.0.1'.split(' - ')

Comments

2

You can try this:

import re
s = "10-24-2017 10:09:18.218 - my_test - INFO - My Automation version 0.0.1"
final_string = re.split("\s-\s", s)

Output:

['10-24-2017 10:09:18.218', 'my_test', 'INFO', 'My Automation version 0.0.1']

Comments

1

You can just use normal split function.

test_str = "10-24-2017 10:09:18.218 - my_test - INFO - My Automation version 0.0.1"

print(test_str.split(' - '))

Output:

['10-24-2017 10:09:18.218', 'my_test', 'INFO', 'My Automation version 0.0.1']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.