简体   繁体   中英

Splitting strings into lists and splitting again

I want to split the string

"   510     -9999999  9             99         12             5             [3, 0]        []            [6]       "

(which contains more or less random numbers of whitespaces between the entries) into it's component parts, including the lists within the string. I can get to this

['510', '-9999999', '9', '99', '12', '5', '[3,', '0]', '[]', '[6]']

through using split and replace. However, I then want to reconstitute the lists within the original string so that I can get to

['510', '-9999999', '9', '99', '12', '5', '[3,0]', '[]', '[6]'].

The real problem is that this string is one of many and the lists may contain many, or no components so I have to deal with this is a general way.

I could potentially search for '[' , then search for ']' to close up the list but, as I don't know the length of any of the lists going in, this seems an inefficient way of doing things.

Any help greatly appreciated!

If lists can't be nested then I think it is possible to preprocess string with:

s = "   510     -9999999  9             99         12             5             [3, 0]        []            [6]       "

opened = False
s_new = ""
for i in s:
    if i == "[":
        opened = True
    if i == "]":
        opened = False
    if not opened or (opened and i != " "):
        s_new += i

And then split it into list:

l = s_new.split()

If lists aren't nested, you can try this:

def mysplit (a):
    return re.split(' +', re.sub('\\[(.*?)\\]', lambda m: '[{}]'.format(m.groups()[0].replace(' ', '')), a))

There is always regex, but you can do it on the cheap like this

>>> import shlex
>>> shlex.split(s.replace('[','"[').replace(']',']"'))
['510', '-9999999', '9', '99', '12', '5', '[3, 0]', '[]', '[6]']

The proper solution would be to use pyparsing module, or even better to control the input source to give you something more sensible like json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM