如何将这样的字符串分成Python中的项目

时间:2014-01-29 03:59:26

标签: python string python-2.7 string-parsing string-split

这是字符串:

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

我试图把它分成这个:

format
db
"this is string a"
0
0Ah

有没有办法在python 2.7中做到这一点?

谢谢!

3 个答案:

答案 0 :(得分:4)

使用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']

你的语法对于尾随的逗号有点不可思议,但你可以非常安全地rstrip表示:

[x.rstrip(',') for x in shlex.split(s)]
Out[20]: ['format', 'db', 'this is string a', '0', '0Ah']

答案 1 :(得分:1)

我确信会有更优雅的答案,但这会起作用并保留引号:

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']

答案 2 :(得分:1)

正则表达式解决方案:

import re

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

输出:

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