2

I have some data of the format

[[prod149090160, prod146340131, prod160860042, prod147040186, prod147860348, prod157590283, prod153940219, prod162460011, prod160410115, prod157370014], [prod162290002, prod151790213, prod159380278, prod154180602, prod160020244, prod161410007, prod155540059, prod152810207, prod152870263, prod159300061], [prod156900051, prod157590288, prod153540027, prod162940222, prod160330181, prod162680033, prod155370061, prod156970034, prod159310027, prod159410165]]

This is a list of list in string format. Is there any simple way to convert this into an in-built python list type.

4
  • eval would do that but it's not safe if the string comes from an non trusted source or could be altered by another program. Commented Oct 21, 2013 at 11:44
  • 1
    You can't simply convert that to a list, because it is not a string representation of a list, the items in the list should have " around them, so should the entire list. Commented Oct 21, 2013 at 11:47
  • if it were a string representation of a list of strings you could have used ast.literal_eval: ast.literal_eval("['a', 'b', 'c']") -> ['a','b','c']. It works for any literal. Commented Oct 21, 2013 at 11:59
  • The items do not have "" around them. That is what is making it hard for me. Commented Oct 21, 2013 at 14:07

4 Answers 4

4

Or PyYAML:

>>> import yaml
>>> s = '[[prod149090160, prod146340131, prod160860042, prod147040186, prod147860348, prod157590283, prod153940219, prod162460011, prod160410115, prod157370014], [prod162290002, prod151790213, prod159380278, prod154180602, prod160020244, prod161410007, prod155540059, prod152810207, prod152870263, prod159300061], [prod156900051, prod157590288, prod153540027, prod162940222, prod160330181, prod162680033, prod155370061, prod156970034, prod159310027, prod159410165]]'
>>> yaml.load(s)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is the most pythonic way of doing things.
3

Use regular expressions:

>>> import re
>>> s = '[[prod149090160, prod146340131, prod160860042, prod147040186, prod147860348, prod157590283, prod153940219, prod162460011, prod160410115, prod157370014], [prod162290002, prod151790213, prod159380278, prod154180602, prod160020244, prod161410007, prod155540059, prod152810207, prod152870263, prod159300061], [prod156900051, prod157590288, prod153540027, prod162940222, prod160330181, prod162680033, prod155370061, prod156970034, prod159310027, prod159410165]]'
>>> groups = re.findall('\[([^\]]*)\]', s[1:-1])
>>> [re.findall('(prod\d+)', group) for group in groups]
[['prod149090160', 'prod146340131', 'prod160860042', 'prod147040186', 'prod147860348', 'prod157590283', 'prod153940219', 'prod162460011', 'prod160410115', 'prod157370014'], ['prod162290002', 'prod151790213', 'prod159380278', 'prod154180602', 'prod160020244', 'prod161410007', 'prod155540059', 'prod152810207', 'prod152870263', 'prod159300061'], ['prod156900051', 'prod157590288', 'prod153540027', 'prod162940222', 'prod160330181', 'prod162680033', 'prod155370061', 'prod156970034', 'prod159310027', 'prod159410165']]

Comments

1

This is what Bakuriu was talking about:

data = '''[["prod149090160", "prod146340131", "prod160860042", "prod147040186",
            "prod147860348", "prod157590283", "prod153940219", "prod162460011",
            "prod160410115", "prod157370014"],
           ["prod162290002", "prod151790213", "prod159380278", "prod154180602",
            "prod160020244", "prod161410007", "prod155540059", "prod152810207",
            "prod152870263", "prod159300061"],
           ["prod156900051", "prod157590288", "prod153540027", "prod162940222",
            "prod160330181", "prod162680033", "prod155370061", "prod156970034",
            "prod159310027", "prod159410165"]]'''

import ast
print ast.literal_eval(data)

Output:

[['prod149090160', 'prod146340131', 'prod160860042', 'prod147040186',
  'prod147860348', 'prod157590283', 'prod153940219', 'prod162460011',
  'prod160410115', 'prod157370014'],
 ['prod162290002', 'prod151790213', 'prod159380278', 'prod154180602',
  'prod160020244', 'prod161410007', 'prod155540059', 'prod152810207',
  'prod152870263', 'prod159300061'],
 ['prod156900051', 'prod157590288', 'prod153540027', 'prod162940222',
  'prod160330181', 'prod162680033', 'prod155370061', 'prod156970034',
  'prod159310027', 'prod159410165']]

The format shown would also be a legal JSON parse-able string:

import json
print json.loads(data)

2 Comments

Unfortunately not JSON, as the strings are "not quoted" properly. YAML works, though.
D'oh, my reading comprehension was offline... I just realized that you use ast to convert the string to the valid JSON string. Sorry...
0
import json
import re

print json.loads(re.sub(r'([^\[\],\s+]+)', r'"\1"', i))

Comments

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.