Python 将元组列表的字符串转换为元组列表

Python Convert a string of tuple list into a tuple list

输入:一个字符串=“[('0.0.0.0',5656), ('0.0.0.0',5656), ('0.0.0.0',5656), ('0.0.0.0',5656 )]"

期望的输出:元组列表[('0.0.0.0',5656), ('0.0.0.0',5656), ('0.0.0.0',5656), ('0.0.0.0',5656 )]

如@Shinratensei 所述,您可以使用 ast

import ast
output = ast.literal_eval("[('0.0.0.0',5656), ('0.0.0.0',5656), ('0.0.0.0',5656), ('0.0.0.0',5656)]")
# [('0.0.0.0', 5656), ('0.0.0.0', 5656), ('0.0.0.0', 5656), ('0.0.0.0', 5656)]