将python中列表的字符串表示形式转换为列表对象

时间:2013-10-21 11:42:40

标签: python list

我有一些格式

的数据
[[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]]

这是字符串格式的列表列表。有没有简单的方法将其转换为内置的python列表类型。

4 个答案:

答案 0 :(得分:4)

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)

答案 1 :(得分:3)

使用正则表达式:

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

答案 2 :(得分:1)

这是Bakuriu所说的:

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)

输出:

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

显示的格式也是合法的JSON可解析字符串:

import json
print json.loads(data)

答案 3 :(得分:0)

import json
import re

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