如何将包含list的字符串转换为真实列表

时间:2014-12-01 13:18:48

标签: python string list

现在我有了unicode对象

u'[
    [
        [
            ["Hello","w_0","w_0,w_10",1],
            [".","w_1","",0," "],
            ["This","w_2","",0," "],
        ]
        [
            ["\u4f60\u597d","w_10","w_0,w_10",1],
            ["\uff0c","w_11","",0],
            ["\u6211","w_12","",0],
        ],
        "http://www.kekenet.com/menu/201206/187574.shtml",154191
    ]
]'

如何将其转换为列表对象,如

[
    [
        [
            ["Hello","w_0","w_0,w_10",1],
            [".","w_1","",0," "],
            ["This","w_2","",0," "],
        ],
        [
            ["\u4f60\u597d","w_10","w_0,w_10",1],
            ["\uff0c","w_11","",0],
            ["\u6211","w_12","",0],
        ],
        "http://www.kekenet.com/menu/201206/187574.shtml",154191
    ],
]

2 个答案:

答案 0 :(得分:3)

您可以使用ast.literal_eval安全地评估包含Python文字的字符串。

from ast import literal_eval

a = '["Hello", "World!", 2]'
b = literal_eval(a)
# ["Hello", "World!", 2]

请注意,字符串只能被破坏:字符串,字节,数字,元组,列表,dicts,集合,布尔值和None(取自文档here

答案 1 :(得分:1)

在这种情况下,你很幸运,因为你的字符串似乎是有效的JSON,所以你可以这样做:

from simplejson import json

value = u'[
    [
        [
            ["Hello","w_0","w_0,w_10",1],
            [".","w_1","",0," "],
            ["This","w_2","",0," "],
        ]
        [
            ["\u4f60\u597d","w_10","w_0,w_10",1],
            ["\uff0c","w_11","",0],
            ["\u6211","w_12","",0],
        ],
        "http://www.kekenet.com/menu/201206/187574.shtml",154191
    ]
]'

array_value = json.loads(value)

为此,它依赖于使用双引号字符串的字符串(根据json规范),即["Hello","w_0","w_0,w_10",1]而不是['Hello','w_0','w_0,w_10',1]

与使用eval的解决方案相比,它更安全...如果此值来自用户输入或其他不受信任的代码,则使用eval非常危险,而json是安全的。

修改
正如已经指出的那样,张贴的字符串是无效的json ......虽然我不知道它是不是一个错字?

问题是:

value = u'[
    [
        [
            ["Hello","w_0","w_0,w_10",1],
            [".","w_1","",0," "],
            ["This","w_2","",0," "],
        ] <-- should be a comma here
        [
            ["\u4f60\u597d","w_10","w_0,w_10",1],