2024-06-02 22:03:18 发布
网友
我的数据库中有一列存储以下格式的字符串:
"['Action', 'Adventure', 'Comedy']"
如何提取电影类型,以便我可以单独使用它们,提取后,我应具有以下内容:
g1 = 'Action' g2 = 'Adventure' g3 = 'Comedy'
你可以试试这个。您可以在每个,处拆分它们,从单词中去掉[] ',并使用元组解包
,
[] '
a="['Action', 'Adventure', 'Comedy']" g1,g2,g3=[i.strip(" []'") for i in a.split(',')]
print(g1,g2,g3) # Action Adventure Comedy
如果您喜欢正则表达式:
import re g = "['Action', 'Adventure', 'Comedy']" g1,g2,g3 = re.findall(r"'(\w+)'",g) print(g1,g2,g3)
试试这个:
inputString = "['Action', 'Adventure', 'Comedy']" # Converting string to list res = inputString.strip('][').split(', ') g1= res[0] g2= res[1] g3= res[2]
有很多方法可以做到这一点
如上所述使用字符串操作
使用ast.literal_eval()
ast.literal_eval()
使用json.loads()
json.loads()
您可以在此处签出所有示例:https://www.geeksforgeeks.org/python-convert-a-string-representation-of-list-into-list/
你可以试试这个。您可以在每个
,
处拆分它们,从单词中去掉[] '
,并使用元组解包如果您喜欢正则表达式:
试试这个:
有很多方法可以做到这一点
如上所述使用字符串操作
使用
ast.literal_eval()
使用
json.loads()
您可以在此处签出所有示例:https://www.geeksforgeeks.org/python-convert-a-string-representation-of-list-into-list/
相关问题 更多 >
编程相关推荐