0

Here is the string:

format db "this is string a", 0, 0Ah

And I am trying to split it into this:

format
db
"this is string a"
0
0Ah

Is there any way can do this in python 2.7?

Thank you!

4
  • is this a fixed string? Commented Jan 29, 2014 at 4:04
  • @Charlesliam Hi, basically I am using Python to parse the assembly code, and I have to deal with stuff like this in parsing the data section of assembly code.. Commented Jan 29, 2014 at 4:05
  • 1
    do you need those quotes to remain on the third string? Commented Jan 29, 2014 at 4:08
  • @roippi No, not necessary. Commented Jan 29, 2014 at 4:09

3 Answers 3

4

Use shlex.split:

s = 'format db "this is string a", 0, 0Ah'

import shlex

shlex.split(s)
Out[18]: ['format', 'db', 'this is string a,', '0,', '0Ah']

Your grammar is a little wonky with the trailing commas, but you can pretty safely rstrip that out:

[x.rstrip(',') for x in shlex.split(s)]
Out[20]: ['format', 'db', 'this is string a', '0', '0Ah']
Sign up to request clarification or add additional context in comments.

3 Comments

shlex.split is much better than re.split
I agree, see how ugly my answer is.
groovy! Thank you roippi!
1

I'm sure there will be more elegant answers, but this'll work and preserve the quotes:

def parse(s):
    s = s.split(', ')
    stack = []
    new = ''
    inQuotes = False
    for char in s[0]:
        if char == '"':
            inQuotes = True
        if not inQuotes:
            if not char == ' ':
                new += char
            else:
                stack.append(new)
                new = ''
        else:
            new += char
    stack.append(new)
    del s[0]
    stack.extend(s)
    return stack

>>> s = 'format db "this is string a", 0, 0Ah'
>>> parse(s)
['format', 'db', '"this is string a"', '0', '0Ah']

Comments

1

A regex solution:

import re

data = 'format db "this is string a", 0, 0Ah'
s = re.findall(r'''(?:[^ '"]+|'(?:[^']|\\.)*'|"(?:[^']|\\.)*")+''', data)
print [x.rstrip(',') for x in s]

output:

['format', 'db', '"this is string a"', '0', '0Ah']

1 Comment

the output is one line?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.