I am trying to convert the following string of a list back to a list.
我试图将列表的以下字符串转换回列表。
[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]
I have tried both eval()
and ast.literal_eval
but for some reason when I print the type of my converted string it is still a string (but with the quotations removed)
我已经尝试了eval()和ast.literal_eval,但出于某种原因,当我打印转换后的字符串的类型时,它仍然是一个字符串(但删除了引号)
I have also tried using json.loads()
我也尝试过使用json.loads()
It seems like no matter how hard I try, I cannot convert this string of a list to a python list!
似乎无论我怎么努力,我都无法将这个列表的字符串转换为python列表!
1 个解决方案
#1
You probably have an additional set of quotation marks, not shown in the question, included in the literal you're evaluating:
您可能还有一组额外的引号,未在问题中显示,包含在您正在评估的文字中:
[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]
is a list, whereas:
是一个列表,而:
"[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]"
is a string.
是一个字符串。
Therefore,
ast.literal_eval("[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]")
returns a list, whereas:
返回一个列表,而:
ast.literal_eval('''"[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]"''')
returns a string. The nested quotes have become verbose in these examples written as Python source code, but perhaps you've read "[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]"
from a file.
返回一个字符串。在这些以Python源代码编写的示例中,嵌套引号已经变得冗长,但是您可能已经从文件中读取了“[('zX7XjZ1Vwai5UbqNDDJ1NQ',570512,[155])]”。
#1
You probably have an additional set of quotation marks, not shown in the question, included in the literal you're evaluating:
您可能还有一组额外的引号,未在问题中显示,包含在您正在评估的文字中:
[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]
is a list, whereas:
是一个列表,而:
"[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]"
is a string.
是一个字符串。
Therefore,
ast.literal_eval("[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]")
returns a list, whereas:
返回一个列表,而:
ast.literal_eval('''"[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]"''')
returns a string. The nested quotes have become verbose in these examples written as Python source code, but perhaps you've read "[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]"
from a file.
返回一个字符串。在这些以Python源代码编写的示例中,嵌套引号已经变得冗长,但是您可能已经从文件中读取了“[('zX7XjZ1Vwai5UbqNDDJ1NQ',570512,[155])]”。