2

我有一些格式的数据

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

4 回答 4

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)
于 2013-10-21T12:01:02.197 回答
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']]
于 2013-10-21T11:54:45.773 回答
1

这就是巴库里乌所说的:

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)
于 2013-10-21T12:07:09.223 回答
0
import json
import re

print json.loads(re.sub(r'([^\[\],\s+]+)', r'"\1"', i))
于 2013-10-21T12:30:03.407 回答