TypeError: cannot concatenate 'str' and 'list' objects. how to convert list to string

TypeError: cannot concatenate 'str' and 'list' objects. how to convert list to string

我有以下 python 代码:

if str(fv["f"])=="AG":
        nf=str(fv["f"])+" "+ in +" ("+"\'"+str(fv["v"])+"\'"+")"

输出为:

nf=AG in ('abc,xyz,ef')

但要求的输出是:

nf=AG in ('abc','xyz','ef')

我试着把 split(",") 放在 str(fv["v"]).split(",") 但它给了我 TypeError: cannot concatenate 'str' and 'list' objects。请帮助我如何获得输出。

这取决于 fv["v"] 的类型,但可能会起作用:

if str(fv["f"]) == "AG":
    nf = "{} in {}".format(fv["f"], tuple(fv["v"]))

当您提供 type(fv["v"]) 的结果时,此答案将得到更正。

str(tuple(str(fv["v"]).split(',')))

可能会给你想要的,但如果你提供更多信息,肯定有更好的写法。

fv['v'] 由什么组成?