0

I have the string below gotten as the output from another process. I need to get only the list of list within the string out that is by eliminating the following 0.466, 0.161 : 19.032 that are the characters at the end of the string.

The string:

route = '''[[(5371.0, 10558.0, {'volume': 8.627265, 'length': 0.17}), (14604.0, 71071.0, {'volume': 15.135287, 'length': 0.28})], [(68127, 68127.0, {'volume': 10.072853, 'length': 0.25}), (5706, 5706.0, {'volume': 27.82635, 'length': 0.25, })], 0.466, 0.161] : 19.032'''

My desired output is the list of list below

[[(5371.0, 10558.0, {'volume': 8.627265, 'length': 0.17}), (14604.0, 71071.0, {'volume': 15.135287, 'length': 0.28})],  [(68127, 68127.0, {'volume': 10.072853, 'length': 0.25}), (5706, 5706.0, {'volume': 27.82635, 'length': 0.25})]] 

I tried this:

result = []
for r in eval(route.split(":")[0]):
    if type(r) == type([]):
        result.append(r)
print result

I got this error:

Traceback (most recent call last):
  File "C:/Users/AMAMIFE/Desktop/obi/SO.py", line 419, in <module>
    for l in eval(route.split(":")[0]):
  File "<string>", line 1
    [[(5371.0, 10558.0, {'volume'
                                ^
SyntaxError: unexpected EOF while parsing

Can I get some help with what I am doing wrong and how to fix it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Nobi
  • 1,113
  • 4
  • 23
  • 41
  • 1
    The colon at the end is **not** the only colon. Look at what you are asking `eval` to parse! Switch to [`ast.literal_eval`](https://docs.python.org/2/library/ast.html#ast.literal_eval) and look into [`str.rsplit`](https://docs.python.org/2/library/stdtypes.html#str.rsplit). – jonrsharpe Jul 16 '14 at 12:38
  • 1
    @sundarnatarajСундар no, it isn't. – jonrsharpe Jul 16 '14 at 12:38
  • @nobi hi, y are u saving the total object to a file or r u recieving that from some where – sundar nataraj Jul 16 '14 at 12:44
  • @sundarnatarajСундар hello, yes I am receiing it from somewhere else – Nobi Jul 16 '14 at 12:45

1 Answers1

4

You should use ast.literal_eval, not eval (which is bad practice).

Note that there are multiple colons in your string, some of which are valid syntax. You want to split only on the last, so use str.rsplit and set maxsplit to 1:

>>> from ast import literal_eval
>>> route = "[[(5371.0, 10558.0, {'volume': 8.627265, 'length': 0.17}), (14604.0, 71071.0, {'volume': 15.135287, 'length': 0.28})], [(68127, 68127.0, {'volume': 10.072853, 'length': 0.25}), (5706, 5706.0, {'volume': 27.82635, 'length': 0.25, })], 0.466, 0.161] : 19.032"
>>> literal_eval(route.rsplit(":", 1)[0])
[[(5371.0, 10558.0, {'volume': 8.627265, 'length': 0.17}), (14604.0, 71071.0, {'volume': 15.135287, 'length': 0.28})], [(68127, 68127.0, {'volume': 10.072853, 'length': 0.25}), (5706, 5706.0, {'volume': 27.82635, 'length': 0.25})], 0.466, 0.161]

As you don't want the last two items in the list, use a slice to remove them:

>>> literal_eval(route.rsplit(":", 1)[0])[:-2]
[[(5371.0, 10558.0, {'volume': 8.627265, 'length': 0.17}), (14604.0, 71071.0, {'volume': 15.135287, 'length': 0.28})], [(68127, 68127.0, {'volume': 10.072853, 'length': 0.25}), (5706, 5706.0, {'volume': 27.82635, 'length': 0.25})]]   
Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437