gpt4 book ai didi

python - 将字符串转换为python中的字符串列表

转载 作者:行者123 更新时间:2023-12-05 08:46:24 24 4
gpt4 key购买 nike

我有一个从 .csv 中提取的字符串,格式如下:

str = "[point, contextual, point]"

我想做的是将其转换为以下格式的列表:

str = ["point", "contextual", "point"]

我该怎么做?我尝试使用 json.loads(str) 但出现错误:

json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

最佳答案

你可以使用:

my_str = "[point, contextual, point]"
my_str[1:-1].split(', ') # remove first and last characters ([]) and split on ", "

注意。不要使用 str 作为变量名,这会覆盖 str内置

关于python - 将字符串转换为python中的字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69605525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com