1

A file contains lines like this:

'MEASUREMENT machine.b_runs "Bit \"machine runs\" Bit"'

What I want is to split it into a list which is like:

list = ['\'MEASUREMENT', 'machine.b_runs', 'Bit \"machine runs\" Bit']

So that the line is split by ' ' except the words between double quotes, by ignoring \"

How do I do this in python?

3
  • You can split a string with a regular expression and remove the delimiters: stackoverflow.com/questions/4697006/… Commented Nov 19, 2015 at 8:46
  • is there any possiblity of your strin like \"\\"Bit \"machine runs\" Bit" ? Commented Nov 19, 2015 at 8:46
  • 1
    Your example string should end with a single quote. Our start with a double quote. Commented Nov 19, 2015 at 8:58

1 Answer 1

1
x=r'MEASUREMENT machine.b_runs "Bit \"machine runs\" Bit"'
print re.split(r'\s(?=(?:(?:[^"]|\\")*(?<!\\)"(?:[^"]|\\")*(?<!\\)")*(?:[^"]|\\")*$)',x)

You can try something like this.

Output:['MEASUREMENT', 'machine.b_runs', '"Bit \\"machine runs\\" Bit"']

Sign up to request clarification or add additional context in comments.

2 Comments

Can you maybe explain, what r'\s(?=(?:(?:[^"]|\\")*(?<!\\)"(?:[^"]|\\")*(?<!\\)")*(?:[^"]|\\")*$)' does?
@Itaipu Its a lookahead stating find a space which has a pair of " ahead of it. The " should not have \ before it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.